21

I was wondering if anybody had a quick and dirty jquery method that will check if a value is numeric or not? I was thinking about using a regex type method to check the value, if not do not submit the form. I was using jquery validation, but I am running into issues just loading jquery validation.

I just have one value that I want to make sure is numeric.

2
  • 2
    See several others e.g stackoverflow.com/questions/9716468/… Commented Jan 22, 2013 at 17:16
  • You can't do a simple parse as it will sometimes return a number from a non-numeric string - try console.log(parseInt("10px", 10)) Commented Jan 22, 2013 at 17:16

1 Answer 1

58

Sure, use jQuery's isNumeric() function.

$.isNumeric("-10");  // true
$.isNumeric(16);     // true
$.isNumeric(0xFF);   // true
$.isNumeric("0xFF"); // true
$.isNumeric("8e5");  // true (exponential notation string)
$.isNumeric(3.1415); // true
$.isNumeric(+10);    // true
$.isNumeric(0144);   // true (octal integer literal)
$.isNumeric("");     // false
$.isNumeric({});     // false (empty object)
$.isNumeric(NaN);    // false
$.isNumeric(null);   // false
$.isNumeric(true);   // false
$.isNumeric(Infinity); // false
$.isNumeric(undefined); // false
Sign up to request clarification or add additional context in comments.

5 Comments

some times it will give TypeError: $.isNumeric is not a function then use jquery.isNumeric() function
also getting a TypeError
+1 because you write in comments the result of each call compared to jquery web site documentation.
Note: $.isNumeric("1,234"); // false
Note: This API has been deprecated in jQuery 3.3.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.