Alexa Adrian
Alexa Adrian

Reputation: 1858

How to validate a decimal number in Asp.Net?

I need to validate a decimal number in asp.net. I thought to use a RegularExpressionValidator. In case you have another idea just suggest me. The number must match numeric(4,1) so accepted values would be:

1; 12; 123; 123,1; (not good: 1234; 12,34; 1,234)

I tried to use this expression:

^\d{1,3}(\,\d{0,1})$  

but something is not good with this one.

Upvotes: 0

Views: 2308

Answers (1)

stema
stema

Reputation: 92986

If you have a comma, then the following digit is not optional, so you need to make the whole group optional, not only the digit.

^\d{1,3}(,\d)?$

See it here on Regexr

? is short for {0,1}

Upvotes: 1

Related Questions