Pooja Pooja - 1 year ago 60
Javascript Question

Regex expression for numbers with commas and optional .00

I want to make a regex pattern for number input. When user enter numbers the comma is automatically separate entered number like 1,424 or 23,232. This is working fine with regex pattern


But the problem is that this pattern allowed dot(.) between numbers. I want to make regex expression like the input can allowed .00 ate the end of numbers not between numbers. But the .00 is also optional.
Allowed number formats are below:


Not allowed number formats are below:


I spend lost of time on same but does't get any solution. Please share yours ideas. Thanks in advance.

Answer Source

The very, very simple answer would be /^[0-9,]*(\.00)?$/. I.e., add an optional .00 suffix, remove support for . literal before the optional part.

As commented above you could go for something more fancy: ^(0|[1-9]\d{0,2}(,?\d{3})*)(\.00)?$

This will behave as commented:

0 // OK
01 // Not OK, must start with 1-9 if not 0 or 0.00
1,1,1.00 // Not OK, groups must be 3 digits, if used
0,000.00 // Not OK, should be 0.00
1,000 // OK
123312131256457.00 // OK, groups are optional
1233121312564 // OK, decimals (.00) are optional
9,223,372 // OK
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download