javascript - Regular validation of numbers or decimals, definitely a challenge
大家讲道理2017-05-19 10:16:45
0
6
679
1. Only regular expressions can be used to verify 2. When the number is a decimal, it needs to be verified with two decimal places 3. It cannot be 0, 0.00 4. It can be 0.10, 0.11, 1, Number type like 0.01
Is this an interview question? You can solve it with Math.round
Give you a reference/q/10...
Update
^[-+]?([1-9]+)|(\d+\.(\d{0,1}[1-9])|(\[1-9][0-9]))$
0 - false
0.01 - true
0.00 - false
0.10 -
falsetrueUpdate #2
Starts with a number except 0.
02.01 - false
2.01 - true
2.00 - false
2.10 - true
/^([0-9]+.[0-9]{0,2}|[1-9])*$/
/^[+-]?\d+(\.\d{2})?$/
or
/^[+-]?\d+(?:\.\d{2})?$/