JRS: In article <
[email protected]>, seen in
Erwin Moller <since_humans_read_this_I_am_spa
(e-mail address removed)> posted at Thu, 8 Jul 2004 14:33:11 :
have a look at parseInt(s, radix)
where s is the string to be parsed and radix is optional, but you should use
it to test for hexadecimal strings.
But parseInt(S, 10) will accept "10 bananas", which is not a decimal
number. It also accepts 1.999 as meaning 1.
remember that the function parseInt will return NaN (Not A Number) when the
conversion cannot be done.
Input validation is best done with a RegExp; see <URL:
http://www.merlyn.
demon.co.uk/js-valid.htm>.
decimal: /^\d+$/
hexadecimal: /^[0-9a-z]+$/i
Use {m,n} where m & n are integers to control the number of digits.
In evaluating the hex string, you may need to prefix it with "0x".
Remember : the aim of the authors of javascript was to attach some
meaning to as many forms of input as possible (dates being a good
example); the aim of the programmer needs to be to ensure that only
unambiguous input of suitable value is accepted, and to interpret it
correctly.