I believe it would be through the use of a regular expression. An expression that satisfies all your proposed tests would be:
^-?\d*\.?\d+$
Example in jsFiddle . Explaining:
-
^
start of string
-
-?
with or without forward
-
\d*
zero or more digits (so that .42
validates, it is important to accept zero digits before the point)
-
\.?
with or without dot
-
\d+
one or more digits
-
$
end of string
Other expressions could be used if you wanted to accept a larger range of numbers - such as grouping thousands using the comma (American standard), permitting scientific notation using e
(common in programming languages), allowing% in front of only one less, etc. And an "obvious" problem with the proposed expression is that it does not reject numbers with leading zeros.
In the end, it's a matter of accurately identifying what format a string might be expected to have to be considered "numeric" and adjusting the expression accordingly. It may become a bit large, but in my opinion this is still simpler than trying a parse manual (only if what you consider "number" does not fit into a regular language is that a more sophisticated method becomes necessary.)