c# - Regex in PreviewTextInput: only decimals between 0.0 and 1.0 -
i'd have regex, allows digits between 0.0 , 1.0 in textbox.
but should in method previewtextinput (c#, wpf project)
so normal regex doesn't work
regex regex = new regex(@"^0|0.0|0\.[0-9]*|1\.0|1$");
i've found regex, allows decimals in previewtextinput method:
regex regex = new regex("^[.][0-9]+$|^[0-1.]*[.,]{0,1}[0-9]*$");
how can change regex accept decimals between 0-1?
thanks.
my method decimals:
private void tb_previewtextinput(object sender, textcompositioneventargs e) { regex regex = new regex("^[.][0-9]+$|^[0-1.]*[.,]{0,1}[0-9]*$"); e.handled = !regex.ismatch((sender textbox).text.insert((sender textbox).selectionstart, e.text)); }
my method decimals between 0-1 (doesn't work):
private void tb_surface_previewtextinput(object sender, textcompositioneventargs e) { regex regex = new regex(@"^(0?\.[0-9]+|1\.0)$"); e.handled = !regex.ismatch(e.text); // e.handled = !regex.ismatch((sender textbox).text.insert((sender textbox).selectionstart, e.text)); }
@"^(0(?:\.\d+)?|1(?:\.0+)?)$"
0
, 1
can matched,if don't want match them,and must have digits after dot ,you can use
@"^(0\.\d+|1\.0+)$"
the code below want,and need remove last dot
when textbox lost focus,such str.trim('.')
private void tb_surface_previewtextinput(object sender, textcompositioneventargs e) { var patten = @"^(0(\.\d*)?|1(\.0*)?)$"; regex regex = new regex(patten); e.handled = !regex.ismatch(e.text); // e.handled = !regex.ismatch((sender textbox).text.insert((sender textbox).selectionstart, e.text)); }
Comments
Post a Comment