JavaScript regular expression for validating numeric data
HTML inputs like Number of Products, Quantity, Aggregate etc will be in numeric format. Before we accept such input, we have to make sure the input is numeric.
How to validate HTML Form Input Numeric field ?
isNumber(input)Returns true if input is a number
/*
Returns true if the value is number
*/
function isNumber(n) {
return /^-?\d+\.?\d*$/.test(n);
}
console.log(isNumber(5)); // true
console.log(isNumber(5.6)); // true
console.log(isNumber("")); // false
console.log(isNumber("aaa")); // false
console.log(isNumber("5aaa5")); // false
Use comments section for any queries.