[javascript] Pure JavaScript: a function like jQuery's isNumeric()

Is there is any function like isNumeric in pure JavaScript?

I know jQuery has this function to check the integers.

This question is related to javascript

The answer is


function IsNumeric(val) {
    return Number(parseFloat(val)) === val;
}

isFinite(String(n)) returns true for n=0 or '0', '1.1' or 1.1,

but false for '1 dog' or '1,2,3,4', +- Infinity and any NaN values.


This should help:

function isNumber(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}

Very good link: Validate decimal numbers in JavaScript - IsNumeric()


There is Javascript function isNaN which will do that.

isNaN(90)
=>false

so you can check numeric by

!isNaN(90)
=>true

var str = 'test343',
    isNumeric = /^[-+]?(\d+|\d+\.\d*|\d*\.\d+)$/;

isNumeric.test(str);