I have a form text field that has a KeyUp event. On KeyUp I\'m ignoring anthing but numbers, the period, backspace, delete and cursor keys. So, the only thing in the field ca
Some people will suggest regexes, but I think a small function is better suited for such a validation:
function validate(x) {
var parts = x.split(".");
if (typeof parts[1] == "string" && (parts[1].length == 0 || parts[1].length > 2))
return false;
var n = parseFloat(x);
if (isNaN(n))
return false;
if (n < 0 || n > 100)
return false;
return true;
}
console.log(validate("95"));
console.log(validate("95."));
console.log(validate("95.0"));
console.log(validate("95.00"));
console.log(validate("95.000"));
console.log(validate("101.01"));
console.log(validate("101"));
Live example