Monday, November 7, 2011

jQuery.isNumeric in jQuery 1.7

As mentioned in my previous post that jQuery 1.7 is released and ready to use. With jQuery 1.7, a new function is introduced which is called "isNumeric()". This is a very handy and small utility introduced in 1.7.As the name of the function clearly suggest that it checks whether the passed value is numeric or not. If it is numeric then it returns true, otherwise false.

This function is really useful as it also returns true for negative numbers, decimal numbers, octal integer, Hex numbers and exponential strings. It saves lots of line of your codes and you don't have to worry about the regular expression as well to validate the number.
$.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(undefined); // false
See live Demo and Code.
Feel free to contact me for any help related to jQuery, I will gladly help you.