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
"To return true / false replace return result;

with return result !== null; "

Wouldn't

return result || false;

also work?
# Posted By Ryan Pfister | 8/26/08 6:13 AM
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
# Posted By Daniel Shaw | 9/18/08 11:54 AM
I'd prefer this:

function is_numeric(x) {
return (x!=null && !isNaN(x));
}
# Posted By Alexander Seebacher | 12/1/08 12:37 AM
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
# Posted By David Reabow | 4/24/09 6:23 AM
isNaN(x) returns true if x is Not a Number, otherwise false.
Are there any gotchas with isNaN(x) that I'm not seeing?
# Posted By Osiris432000 | 6/15/09 3:15 PM
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.
# Posted By Josh Berke | 3/4/10 10:41 AM
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. :)
# Posted By Jake Munson | 3/4/10 10:57 AM
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.
# Posted By Josh Berke | 3/4/10 1:57 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.9. Contact Blog Owner