JRS: In article <40ed3f03$1$568$>, seen in
news:comp.lang.javascript, Erwin Moller <since_humans_read_this_I_am_spa
> posted at Thu, 8 Jul 2004 14:33:11 :
>Cogito wrote:
>
>> In a form I have two text fields. In one of them the user enters a
>> decimal number and in the other a hexadecimal number. What code can be
>> used to validate that the entered text is numeric and to produce a
>> warning when it is not.
>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.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> JL / RC : FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.