2

I need a validation RegEx for decimal. It should allow up to 5 digits after decimal. Allow:

1    
1.0    
12.0    
12.01    
123.01    
1,123.01    
1,123.013
21,123.01234    
3,21,123.01234

How Can I do regex for this?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
James123
  • 11,184
  • 66
  • 189
  • 343

4 Answers4

4

http://regexlib.com/Search.aspx?k=decimal

Andrey
  • 59,039
  • 12
  • 119
  • 163
3
^[\d,]+(\.\d{1,5})?$
Yuriy Faktorovich
  • 67,283
  • 14
  • 105
  • 142
2

Do you want to validate the positions of the commas? If so, this works for the Indian numbering system that you seem to be using:

^(?:\d{1,2},(?:\d{2},)*\d{3}|\d{1,3})(\.\d{1,5})?$

If you want to permit commas in the integer portion but don't care about their positions (except that it can't start or end with a comma), this will do:

^\d+(?:,\d+)*(\.\d{1,5})?$
Alan Moore
  • 73,866
  • 12
  • 100
  • 156
0

There an excellent library of commonly used regular expressions at http://regexlib.com/.

You an use a standard expression like: ^\$[0-9]+(\.[0-9][0-9])?$ or one of the alternative versions that do much the same thing, depending on the exact flavor you want to match.

LBushkin
  • 129,300
  • 32
  • 216
  • 265