Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression for 3 decimal point

Tags:

regex

asp.net

I need a regular expression that satisfy these rules:

  1. The maximum number of decimal point is 3 but a number with no decimal point (e.g 12) should be accepted too
  2. the value must be at least 0
  3. the value must be less or equal to 99999999999.999
  4. the radix point is DOT (e.g 2.5, not 2,5)

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

like image 673
ixora Avatar asked Dec 03 '22 01:12

ixora


1 Answers

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
like image 122
Tim Pietzcker Avatar answered Dec 17 '22 15:12

Tim Pietzcker