I need to validate a numeric string with JavaScript, to ensure the number has exactly two decimal places.
The validation will pass only if
- the number has precisely two decimal places
- there is at least one digit before the decimal point. (could be zero)
- the number before the decimal point can not begin with more than one zero.
Valid numbers:
0.01
0.12
111.23
1234.56
012345.67
123.00
0.00
Invalid numbers:
.12
1.1
0.0
00.00
1234.
1234.567
1234
00123.45
abcd.12
12a4.56
1234.5A
I have tried the regular expression [0-9][\.][0-9][0-9]$, but it allows letters before decimal point like 12a4.56.
.matches any character, it does not do what you think it does. You have to escape it. Also, you have two more errors; tryinstead, or even better, use
\dfor decimal digits: