I searched the web and came up with: /(^100(\\.0{1,2})?$)|(^([1-9]([0-9])?|0)(\\.[0-9]{1,2})?$)/
The problem is it accepts 0.
Perhaps you are thinking about the problem wrongly. A better method if possible would be to convert the string to a double and perform the check using arithmetic operators .
const examplePercentage = '0.5%';
const percentageAsFloat = parseFloat(examplePercentage); // 0.5
const isValid = percentageAsFloat > 0 && percentageAsFloat <= 100; // true
Try this one:
^0*(100(\.0{1,2})?|[1-9][0-9]?(\.[0-9]{1,2})?|0\.(0[1-9]|[1-9][0-9]?))$
It handles >= 1
different from < 1
.
But I would go with parsing the number and checking it's value.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.