0

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 5

5

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+$

Sign up to request clarification or add additional context in comments.

2 Comments

Why wouldn't you use a regular expression? Too much?
I am always hesitant to create a regular expression that will validate input for which there exists a library parsing function (int, decimal, datetime, etc.).
0

just the regex would be

`\d*` 

Comments

0

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

0

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

0

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> 

Comments