
checking to make sure that an input is a number
Tue, 26 Nov 2002 13:50:49 :-
Quote:
>I need to know how to check if an input is a number.
>Help.
>Not just an integer but a positive number
You probably do not need to know that. For example, "positive number"
includes 3.14159E0 and 5E+300 - do you need to accept those?
If what you are doing is in essence inputting a count, then you can call
and test for a number of 1 to N decimal digits, using a RegExp such as
/\d{1,8}/.
For the case of non-integer, you should require at least one digit on
each side of the decimal point; and you should perhaps require the
decimal point. Allowing numbers to have a decimal point at one end
becomes error-prone.
If the input is a currency, you should probably not allow just a single
digit after a decimal point; that is liable to be an error.
Beware of allowing an empty string, unless specified permissible.
--
<URL:http://www.jibbering.com/faq/> FAQ for comp.lang.javascript by Jim Ley.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.