Regular expression for checking numeric values
6 answers
Why not use CompareValidator to make sure it's a number?
<asp:TextBox ID="numberTextBox" runat="server" />
<asp:CompareValidator ID="validator" runat="server" ControlToValidate="numberTextBox" Operator="DataTypeCheck" Type="Double" ErrorMessage="Value must be a number" />
+7
source to share
As you can see from various answers, Regexes can add unnecessary complexity. float.TryParse()
will tell you for sure if the value can be made float or not.
It also takes into account regional settings on the user's machine, which regular expressions won't (or will be ugly if they try).
I would consider using something like this instead of Regex:
bool isValid = float.TryParse(textbox1.Text);
+4
source to share