JS regex percentage

An input box, enter a percentage value, requires:

The minimum is 100%, and the maximum number of decimal places is 2.

//最小100%, 最多两位小数
var reg = /^[1-9]{1}\d{2,}\.?\d{0,2}$/;

if (inputValue != "") {
   if (!reg.test(inputValue)) {
       //不符合
       return false;
   }
}

Break it down:

^[1-9]{1}

The first number at the beginning needs to be 1-9, that is, it cannot be 0

 

\d{2,}

After the first digit, match at least 2 digits. Can be 0-9. That is, the minimum 3 digits minimum 100%

\.?

Match 0 or 1 ., can have no decimal point

\d{0,2}$

end with one or two digits after the dot

Guess you like

Origin blog.csdn.net/qq_25148525/article/details/126349068