EDIT: Blender's updated answer is the right answer here if you're just checking a single character (namely !isNaN(parseInt(c, 10))
). My answer below is a good solution if you want to test whole strings.
Here is jQuery's isNumeric
implementation (in pure JavaScript), which works against full strings:
function isNumeric(s) {
return !isNaN(s - parseFloat(s));
}
The comment for this function reads:
// parseFloat NaNs numeric-cast false positives (null|true|false|"")
// ...but misinterprets leading-number strings, particularly hex literals ("0x...")
// subtraction forces infinities to NaN
I think we can trust that these chaps have spent quite a bit of time on this!