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

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(6)
滿天的星座

Is this an interview question? You can solve it with Math.round

PHPzhong

Give you a reference/q/10...

淡淡烟草味
^[-+]?\d+(\.\d{0,1}[1-9])?$

Update
^[-+]?([1-9]+)|(\d+\.(\d{0,1}[1-9])|(\[1-9][0-9]))$

^(([1-9]+)|(\d+\.((\d{0,1}[1-9])|([1-9][0-9]))))$

0 - false
0.01 - true
0.00 - false
0.10 - falsetrue

Update #2

Starts with a number except 0.

^((^[1-9][0-9]*)|((^[1-9][0-9]*)\.((\d{0,1}[1-9])|([1-9][0-9]))))$

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})?$/

世界只因有你
var arr = [0, 0.00, 0.10, 0.11, 1, 0.01, -0, -0.00, -0.10, +0.11, -1, -0.01];
var reg = /^[+-]?([1-9]{1}\d?|[1-9]+.\d{2}|0.[1-9]{1}\d{1}|0.0[1-9]{1})$/;
for (let i = 0, len = arr.length; i < len; i++) {
    console.log(arr[i], reg.test(arr[i]));
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template