I am trying to validate a price field in Javascript.
The value can only be numbers, must have 1 decimal point, and must have 2 decimal places after it. Only 7 digits can be in front of the decimal point. Like: 1000000.00
Accepted:
123.00
1.01
0.01
4576.23
1234567.00
1.00
Not accepted:
0.00 (Cannot be free)
0.1 (not 2 decimal places)
1.0 (not 2 decimal places)
01.01 (Cannot start with 0)
12345678.00 (too many digits)
123 (no decimal point and 2 places)
-123.12 (negative, and unacceptable character)
123.123 (too many places)
I am unsure how to approach this problem and any help would be appreciated. A simple guide on how to do write my own regex would be helpful too as English is not my strong point. Thanks in advance.
Here's what I tried on my own: /^[0-9]+.[0-9]{2}$/ But I am unsure how to approach the 0 and length problem.