Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

Is there is any function like isNumeric in pure JavaScript?

I know jQuery has this function to check the integers.

like image 254
Rinto George Avatar asked Mar 15 '12 08:03

Rinto George


People also ask

Is numeric function JavaScript?

isNumeric() method checks whether its argument represents a numeric value. If so, it returns true . Otherwise it returns false . The argument can be of any type.

Is number integer JavaScript?

html. The Number. isInteger() method in JavaScript is used to check whether the value passed to it is an integer or not. It returns true if the passed value is an integer, otherwise, it returns false.

How check textbox value is integer or not in jQuery?

Answer: Use the jQuery. isNumeric() method isNumeric() method to check whether a value is numeric or a number. The $. isNumeric() returns true only if the argument is of type number, or if it's of type string and it can be coerced into finite numbers, otherwise it returns false .


2 Answers

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.

like image 82
Sudhir Bastakoti Avatar answered Sep 20 '22 18:09

Sudhir Bastakoti


This should help:

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

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

like image 23
Tats_innit Avatar answered Sep 21 '22 18:09

Tats_innit