Javascript percentage validation

I propose this one:

(^100(\.0{1,2})?$)|(^([1-9]([0-9])?|0)(\.[0-9]{1,2})?$)

It matches 100, 100.0 and 100.00 using this part

^100(\.0{1,2})?$

and numbers like 0, 15, 99, 3.1, 21.67 using

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

Note what leading zeros are prohibited, but trailing zeros are allowed (though no more than two decimal places).


Rather than using regular expressions for this, I would simply convert the user's entered number to a floating point value, and then check for the range you want (0 to 100). Trying to do numeric range validation with regular expressions is almost always the wrong tool for the job.

var x = parseFloat(str);
if (isNaN(x) || x < 0 || x > 100) {
    // value is out of range
}