What is JQuery IsNumeric like function in Javascript?


Hello, I am working with front-end development and would like to use pure javascript instead of using jQuery $.IsNumeric(), so what is possible equivalent function of IsNumeric in Javascript? To validate if string/decimal value is number or not.


Asked by:- neena
0
: 130 At:- 10/31/2022 3:40:56 PM
Javascript IsNumeric







1 Answers
profileImage Answered by:- vikas_jk

There is no IsNumeric similar built-in function in Javascript, but you can create your own method

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

You can test this in Javascript

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

console.log(isNumeric( "-10" )); // true
console.log(isNumeric( 0xFF )); // true
console.log(isNumeric( "3.1415" ));//true
console.log(isNumeric( +10 )); // true


console.log(isNumeric( {} ));//false
console.log(isNumeric( NaN ));//false
console.log(isNumeric( null ));//false
console.log(isNumeric( true ));//false

You can verify it on fiddle: https://jsfiddle.net/6m9qobs1/

Hope it helps.

1
At:- 10/31/2022 3:49:19 PM
Ohh useful and detailed answer, thanks for custom javascript IsNumeric function code and samples. 0
By : neena - at :- 10/31/2022 3:50:09 PM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use