Thank you for clarification. It finally looks like Regex is not helpful at all.
Before entering data, perform filtering, to filter out everything except decimal digits, '.' and backspace (code point 8). Unfortunately, masked edit won't help much. For example, if this is
System.Windows.Controls.TextBox
, either handle the event
TextInput
or overload
OnTextInput
method. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.controls.textbox%28v=vs.95%29.aspx[
^].
Set
System.Windows.Input.TextCompositionEventArgs.Handled
to cancel input of a wrong character:
http://msdn.microsoft.com/en-us/library/system.windows.input.textcompositioneventargs%28v=vs.95%29.aspx[
^].
After entering data, at the moment when the data is about to be used, or earlier, for example, on loosing focus, perform the validation of input. Don't care about extra digits. Simply perform
double.TryParse
. If it is not failed, you can additionally validate that the value is in required range or meet any other math criteria.
Everything else would be unreliable. The
TryParse
methods truly "know" if the string is valid for parsing to one or another numeric type. There are too many variants, and algorithm of parsing is complex enough; you don't want to emulate it.
—SA