Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Testing for Numeric Data

Reply
Thread Tools

Testing for Numeric Data

 
 
Cogito
Guest
Posts: n/a
 
      07-08-2004
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.
 
Reply With Quote
 
 
 
 
Erwin Moller
Guest
Posts: n/a
 
      07-08-2004
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.


Hi Cogito ,


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.

remember that the function parseInt will return NaN (Not A Number) when the
conversion cannot be done.

Good luck.

Regards,
Erwin Moller
 
Reply With Quote
 
 
 
 
Selrak
Guest
Posts: n/a
 
      07-08-2004
Be careful with parseInt, in IE it have a bug, and for 08 or 09 don't have a
good response. If you can, use parseFloat.

Selrak


"Erwin Moller"
<since_humans_read_this_I_am_spammed_too_much@spam yourself.com> escribió en
el mensaje news:40ed3f03$1$568$...
> 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.

>
> Hi Cogito ,
>
>
> 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.
>
> remember that the function parseInt will return NaN (Not A Number) when

the
> conversion cannot be done.
>
> Good luck.
>
> Regards,
> Erwin Moller



 
Reply With Quote
 
Randy Webb
Guest
Posts: n/a
 
      07-08-2004
Selrak wrote:
> Be careful with parseInt, in IE it have a bug, and for 08 or 09 don't have a
> good response. If you can, use parseFloat.


IE doesn't have a "bug" in parseInt, its a bug in the implementation of
the people using it.

http://www.jibbering.com/faq/#FAQ4_12

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/
 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      07-08-2004
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.
 
Reply With Quote
 
Lee
Guest
Posts: n/a
 
      07-08-2004
Erwin Moller said:
>
>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.

>
>Hi Cogito ,
>
>
>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.
>
>remember that the function parseInt will return NaN (Not A Number) when the
>conversion cannot be done.


That depends on what you consider to be "the conversion".
parseInt("action",16) will quite contentedly convert that
invalid input into the number 172.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Testing for Numeric Digits Mike Copeland C++ 11 02-28-2012 07:43 PM
Regex testing and UTF8 awarenes or Regex and numeric pattern matching sln@netherlands.com Perl Misc 2 03-10-2009 03:51 AM
int to numeric numeric(18,2) ? jobs ASP .Net 2 07-22-2007 12:32 AM
Arithmetic overflow error converting numeric to data type numeric. darrel ASP .Net 4 07-19-2007 09:57 PM
check if string contains numeric, and check string length of numeric value ief@specialfruit.be C++ 5 06-30-2005 01:08 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57