Paddy
Paddy

Reputation: 936

Regular Expression with specific required numbers on both sides of decimal point

I am trying to figure out a regular expression for an asp.net RequiredFieldValidator that validates decimal values from 1.001 to 99.9999.

This means every value greater than or equal to 1.001 and less than or equal to 99.9999.

The closest I have managed is:

(?=\d+(?:\.\d+)?$)(?![0\.]+$).{1,7}$

This still allows 0.1 and 1.0001, how do I prevent these values?

Upvotes: 1

Views: 84

Answers (2)

Ulugbek Umirov
Ulugbek Umirov

Reputation: 12807

You can use negative lookaheads for that task:

^0*(?!1(?:\.0+)?$)(?!1\.000)(?!99\.9999.*[1-9])[1-9][0-9]?(?:\.[0-9]+)?$

Regular expression visualization

Demo

Upvotes: 1

lpg
lpg

Reputation: 4937

I don't know asp.net, but maybe you could use something like this?

[1-9][0-9]?\.[0-9]{2}[1-9][0-9]?

However, maybe converting this to float and checking its inside the interval would do the trick, without the use of regexp...

Upvotes: 0

Related Questions