Reputation: 185
I need a regular expression that satisfy these rules:
Sample of valid numbers:
0
2
0.4
78784764.23
45.232
Sample of invalid numbers:
-2
123456789522144
84.2564
I found an example here (http://forums.asp.net/t/1642501.aspx) and have managed to modify it a little bit to make 0 as the minimum value, 99999999999.999 as the maximum value and to accept only DOT as radix point. Here's my modified regex:
^\-?(([0-9]\d?|0\d{1,2})((\.)\d{0,2})?|99999999999.999((\.)0{1,2})?)$
However, I still have problem with the 3 decimal point and it is rather unstable. Can anyone help me on this since I'm basically illiterate when it comes to regex?
Thanks.
EDITED: I'm using ASP Regular Expression Validator
Upvotes: 5
Views: 23120
Reputation: 336168
This is not that difficult:
^[0-9]{1,11}(?:\.[0-9]{1,3})?$
Explanation:
^ # Start of string
[0-9]{1,11} # Match 1-11 digits (i. e. 0-99999999999)
(?: # Try to match...
\. # a decimal point
[0-9]{1,3} # followed by one to three digits (i. e. 0-999)
)? # ...optionally
$ # End of string
Upvotes: 22