The Web Blinders logo

Programming

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.
JavaScript - How to validate credit / debit card expiration date ?
HTML Input Numeric field validation.

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.

Need developers ?

if so, send a message.

thewebblinders@gmail.com

More Programming from our blog

SEARCH FOR ARTICLES