JavaScript isNumeric function

The other day I needed to check a value to make sure it's numeric, using JavaScript (it was an Ajax app). I poked around online and a few sites said that JS doesn't have such a function. I also found a few sites that offered an isNumeric function, but most of them didn't check for decimals and/or negatives. So I created my own using a Regular Expression:

function isNumeric(x) {
// I use this function like this: if (isNumeric(myVar)) { }
// regular expression that validates a value is numeric
var RegExp = /^(-)?(\d*)(\.?)(\d*)$/; // Note: this WILL allow a number that ends in a decimal: -452.
// compare the argument to the RegEx
// the 'match' function returns 0 if the value didn't match
var result = x.match(RegExp);
return result;
}

Comments
Maybe this is a better pattern?
/^[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?\b/

Handles the end decimal and some other things :)
# Posted By Ryan Matsikas | 2/23/06 9:29 AM
oops.. that should be:
/^[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?$/

or

/^[-+]?\d*\.?\d+(?:[eE][-+]?\d+)?$/

if you prefer
# Posted By Ryan Matsikas | 2/23/06 9:56 AM
The biggest difference I see in that is it allows scientific notation. That's a good addition, not usefull to my current application but, but I'll remember to look here if I need it in the future.
# Posted By yacoubean | 2/23/06 6:20 PM
Please note that the function you created, isNumeric, actually is <b>null</b> if no match is found in IE 5.5/6. I would generally expect Firefox to operate the same with respect to javascript, with exception that I have learned not to trust them to be similar enough to work without testing first.

Anyway, kudos on publishing this. I should have remembered it without looking it up.
# Posted By Alex Boese | 5/19/06 11:12 AM
thanks a lot .it solved my problem
# Posted By farnaz | 8/12/06 11:42 PM
Thanks for this script!!
I searched for it!
# Posted By Miky-Man | 8/23/06 10:09 AM
you must add

var result = x.match(RegExp);

//new
if (result==null) result=false;
//new

return result;
# Posted By Pat Nor | 9/14/06 6:47 AM
An alternate way that I use:

function isNumber( value )
{
return isFinite( (value * 1.0) );
}
# Posted By BadBeta | 1/8/07 8:22 AM
I love googling and finding one of my CF peeps with the info. Thanks Jake!
# Posted By Dave Shuck | 1/11/07 7:14 PM
Thanks Jake
# Posted By A user | 1/16/07 4:48 AM
hmm ..
function IsNumeric(n){if(n*1==n)return true;else return false;}
any non numeric value = NaN when multiplied by 1 thus altering the value of n
could use this too...
function IsNumeric(n){if(n*1==NaN)return false;else return true;}
# Posted By Seth | 4/1/07 11:15 AM
Mr. Ryan Matsikas in --> /^[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?\b/ may i know wats --> [eE] is? :)
# Posted By Jeff | 4/9/07 3:18 PM
As far as I know, [eE] is just the letter "e." That makes sense if you're doing mathematical equations. *shrug*
# Posted By Craig | 4/12/07 10:03 AM
Thanks for the script! This is exactly what I was looking for.
# Posted By Pete | 9/6/07 1:14 AM
To return true / false replace return result;

with return result !== null;
# Posted By t-a-g | 9/19/07 11:37 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.9. Contact Blog Owner