pankaj
pankaj

Reputation:

Text box validataion for Decimal value

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?

Upvotes: 0

Views: 10536

Answers (5)

Muhammad Adnan
Muhammad Adnan

Reputation: 1403

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>

Upvotes: 0

Andrew Hare
Andrew Hare

Reputation: 351476

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

Upvotes: 5

CitizenBane
CitizenBane

Reputation: 855

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

Upvotes: 0

Tom Brown
Tom Brown

Reputation: 793

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

Upvotes: 0

Charles Ritchie
Charles Ritchie

Reputation: 2453

just the regex would be

`\d*`

Upvotes: 0

Related Questions