I have an application in which I want to validate a text box that it will take decimal value. I would like to use a regular expression validator for this. What would this expression look like?
5 Answers
I wouldn't use a regular expression - create a custom validator that uses Decimal.TryParse under the covers.
Edit: To be fair to the question, here is a regular expression that would do the trick:
^\d*\.?\d+$
2 Comments
I'd use a CompareValidator - for type decimal And a Required field Validator - so blank is not allowed But regular expression is accepable this link gives some examples http://msdn.microsoft.com/en-us/library/ms998267.aspx
Comments
I say to keep using the Regular Expression Validator, you'll get the added benefit of client-side validation with it.
Here is a list of regular expressions related to decimals: http://regexlib.com/DisplayPatterns.aspx?cattabindex=2&categoryId=3
Comments
I would use a CompareValidator and use Regular Expression
^\d*[0-9](|.\d*[0-9]|,\d*[0-9])?$ This allow all decimal number, exclude all alphanumeric caracter
e.g.
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Only Decimal Value" ValidationExpression="^\d*[0-9](|.\d*[0-9]|,\d*[0-9])?$"> </asp:RegularExpressionValidator>