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 :)
oops.. that should be:
/^[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?$/

or

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

if you prefer
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.
Please note that the function you created, isNumeric, actually is null 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.
thanks a lot .it solved my problem
Thanks for this script!!
I searched for it!
you must add

var result = x.match(RegExp);

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

return result;
An alternate way that I use:

function isNumber( value )
{
return isFinite( (value * 1.0) );
}
I love googling and finding one of my CF peeps with the info. Thanks Jake!
Thanks Jake
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;}
Mr. Ryan Matsikas in --> /^[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?\b/ may i know wats --> [eE] is? :)
As far as I know, [eE] is just the letter "e." That makes sense if you're doing mathematical equations. *shrug*
Thanks for the script! This is exactly what I was looking for.
To return true / false replace return result;

with return result !== null;
"To return true / false replace return result;

with return result !== null; "

Wouldn't

return result || false;

also work?
I too come from a CF background so I started my search looking for this function. However, I think you should use built in language features first and there is one:
isNaN(); // is NOT a number

If you want to alias the CF function then you could do this:
function isNumeric(value) {
return !isNaN(value);
}

Cheers,
Daniel Shaw
http://dshaw.com
I'd prefer this:

function is_numeric(x) {
return (x!=null && !isNaN(x));
}
I have combined various peoples contributions to get this:

String.prototype.trim = function() {
return (this.replace(/^\s+/, '')).replace(/\s+$/, '');
};

String.prototype.isNumeric = function() {
return (this!=null && !isNaN(this) && this.trim()!="");
}

Number.prototype.isNumeric = function() {
return (this!=null && !isNaN(this));
}
// Can add isNumeric to other Constructors if you like.


Usage:

var a = " ";
var b = new Number();
var c = 5;
var d = new Number(); d=null;

a.isNumeric() //returns false
b.isNumeric() //returns true as defaults to 0
c.isNumeric() //returns true
d.isNumeric() //returns false

Regards
David Reabow
isNaN(x) returns true if x is Not a Number, otherwise false.
Are there any gotchas with isNaN(x) that I'm not seeing?
While I like the simplicity of isNaN(parseFloat(+ this.value)) or any of the variants proposed here none of them handle a very common case: 900,000

Putting a comma in there throws all of them off. Now if we talk comma's we could also talk how some cultures use periods but luckily my application is only targeting the states. I think I will stick with ^[-+]?(?:\d*|\d{1,3}(?:,\d{3})*)(?:\.\d+)?$ which lets me validate a wide range of cases.
Josh, thanks for the addition for people that may want to add commas to their. However, to be nit picky (as I so often am), in most programming contexts 900,000 is not a numeric value, it is a string. :)
Yes in many contexts it is but if you are accepting user input and you need to verify that it is indeed a valid numerical representation, then you need to handle comma's either by stripping them or handling them. If your an international application then you'd want want to handle periods as well.