Here I've collected the "good ones" from this page and put them into a simple test pattern for you to evaluate on your own.
For newbies, the console.log is a built in function (available in all modern browsers) that lets you output results to the JavaScript console (dig around, you'll find it) rather than having to output to your HTML page.
var isNumeric = function(val){
// --------------------------
// Recommended
// --------------------------
// jQuery - works rather well
// See CMS's unit test also: http://dl.getdropbox.com/u/35146/js/tests/isNumber.html
return !isNaN(parseFloat(val)) && isFinite(val);
// Aquatic - good and fast, fails the "0x89f" test, but that test is questionable.
//return parseFloat(val)==val;
// --------------------------
// Other quirky options
// --------------------------
// Fails on "", null, newline, tab negative.
//return !isNaN(val);
// user532188 - fails on "0x89f"
//var n2 = val;
//val = parseFloat(val);
//return (val!='NaN' && n2==val);
// Rafael - fails on negative + decimal numbers, may be good for isInt()?
// return ( val % 1 == 0 ) ? true : false;
// pottedmeat - good, but fails on stringy numbers, which may be a good thing for some folks?
//return /^-?(0|[1-9]\d*|(?=\.))(\.\d+)?$/.test(val);
// Haren - passes all
// borrowed from http://www.codetoad.com/javascript/isnumeric.asp
//var RE = /^-{0,1}\d*\.{0,1}\d+$/;
//return RE.test(val);
// YUI - good for strict adherance to number type. Doesn't let stringy numbers through.
//return typeof val === 'number' && isFinite(val);
// user189277 - fails on "" and "\n"
//return ( val >=0 || val < 0);
}
var tests = [0, 1, "0", 0x0, 0x000, "0000", "0x89f", 8e5, 0x23, -0, 0.0, "1.0", 1.0, -1.5, 0.42, '075', "01", '-01', "0.", ".0", "a", "a2", true, false, "#000", '1.2.3', '#abcdef', '', "", "\n", "\t", '-', null, undefined];
for (var i=0; i<tests.length; i++){
console.log( "test " + i + ": " + tests[i] + " \t " + isNumeric(tests[i]) );
}
jQuery.isNumericutility function: api.jquery.com/jQuery.isNumericjQuery.isNumericwill fail the OP's seventh test case (IsNumeric('0x89f') => *false*). I'm not sure if I agree with this test case, however.