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.

SoftwareSavant
  • 9,467
  • 27
  • 121
  • 195
  • 2
    See several others e.g http://stackoverflow.com/questions/9716468/is-there-any-function-like-isnumeric-in-javascript-to-validate-numbers – glosrob Jan 22 '13 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))` – Reinstate Monica Cellio Jan 22 '13 at 17:16

1 Answers1

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
j08691
  • 204,283
  • 31
  • 260
  • 272