Pure JavaScript: a function like jQuery’s isNumeric() [duplicate]

Pure JavaScript: a function like jQuery’s isNumeric() [duplicate]

This question already has an answer here:

Validate decimal numbers in JavaScript – IsNumeric()

48 answers

Is there is any function like isNumeric in pure JavaScript?
I know jQuery has this function to check the integers.

Solutions/Answers:

Solution 1:

There’s no isNumeric() type of function, but you could add your own:

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

NOTE: Since parseInt() is not a proper way to check for numeric it should NOT be used.

Solution 2:

This should help:

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

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

Solution 3:

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

Solution 4:

There is Javascript function isNaN which will do that.

isNaN(90)
=>false

so you can check numeric by

!isNaN(90)
=>true

Solution 5:

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

isNumeric.test(str);

Solution 6:

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.