My problem in a nutshell:
I have a WPF TextBox where a user should only be allowed to enter decimal numbers. I have my validation logic in my TextBox’s PreviewTextInput event handler.
So fare I have tried:
Using TryParse
double number = 0;
bool isSuccessful = double.TryParse(e.Text, out number);
e.Handled = !(number >= 0 && isSuccessful);
Using a Regex: (as per Justin Morgan’s response to this question)
string validNumberFormat = @"^[-+]?(\d{1,3}((,\d{3})*(\.\d+)?|([.\s]\d{3})*(,\d+)?)|\d+([,\.]\d+)?)$";
e.Handled = Regex.Matches(e.Text, validNumberFormat).Count < 1;
Both of the above methods will only allow me to enter numbers, but not the single decimal point.
Any suggestions would be most welcome.
is use a behavior(Blend SDK System.Windows.Interactivity) instead of handling PreviewTextInput directly. i do this because of reusability and also you should know that the space is not handled with TextInput and also Pasting is not handled. btw i think Jonathan and Felice Pollano are right, regarding to your “decimal point” problem.
using