Javascript Regular expression to enforce 2 digits after decimal points

Posted by Manas Saha on Stack Overflow See other posts from Stack Overflow or by Manas Saha
Published on 2012-11-03T10:39:25Z Indexed on 2012/11/03 11:00 UTC
Read the original article Hit count: 291

Filed under:

I need to validate a text box where users are supposed to enter numeric values with 2 decimal places. I am validating the text in client side javascript. The validation will pass only if the number has precisely 2 decimal places, and there is at least 1 digit before the decimal point. (could be zero)

the number before the decimal point can be 0, but can not be multiple zeroes. like 00 or 000

The number before the decimal point can not begin with more than 1 zero.

Example of Passed validation:

 0.01
 0.12
 111.23
 1234.56
 012345.67
 123.00 
 0.00

Example of failed validation

 .12
 1.1
 0.0
 00.00
 1234.
 1234.567
 1234
 00123.45
 abcd.12
 12a4.56
 1234.5A

I have tried with [0-9][.][0-9][0-9]$ But it is allowing alphabets before decimal point. like 12a4.56 Can anyone please fix the expression? thanks a lot in advance.

© Stack Overflow or respective owner

Related posts about JavaScript