471,086 Members | 1,159 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,086 software developers and data experts.

are "07" and "08" really all that different?

I have a web page. It uses JavaScript to test the user's input
before sending it to the server. It frequently tests using isNaN() with
some very interesting results.

The statements in question test values from text boxes using isNaN() and
also checking if they are still the same after parseInt or parseFloat
are performed on them. The second test is because isNaN() will return
false (is a number) for the string "3a".

The curious behavior is that when I enter the string "07" (or any value
less), it returns a number as it should. If I enter the string "08" or
"09", it acts as though I'd entered a non-digit.

I think maybe this whole problem could be solved by using regular
expressions instead of isNaN. I read some dox on RegEx, but didn't
understand much of it. Please help.

To see it, go to
http://ffej2ffej.com/re/start.php

log on as driver 236 with the password 236 then insert new data. Try
"07" and "08" in any of the text boxes and you'll see what I mean when
you try to save. Of course you can see all the javascript by viewing
the source of the frame.
Jul 23 '05 #1
5 1075
*Jeff Sandler* wrote in comp.lang.javascript:
[snip]
The curious behavior is that when I enter the string "07" (or any value
less), it returns a number as it should. If I enter the string "08" or
"09", it acts as though I'd entered a non-digit.

[snip]

See the following regarding the optional radix parameter:
http://msdn.microsoft.com/library/en...thparseint.asp

More info at: http://jibbering.com/faq/#FAQ4_12
--
Andrew Urquhart
- FAQ: http://www.jibbering.com/faq/
- Archive: http://www.google.com/groups?q=comp.lang.javascript
- Contact me: http://andrewu.co.uk/contact/
Jul 23 '05 #2
On Mon, 30 Aug 2004 12:32:31 -0700, Jeff Sandler
<ff*******@dslextreme.com> wrote:
I have a web page. It uses JavaScript to test the user's input before
sending it to the server. It frequently tests using isNaN() with some
very interesting results.
You should check formatted input with regular expressions.

[snip]
The curious behavior is that when I enter the string "07" (or any value
less), it returns a number as it should. If I enter the string "08" or
"09", it acts as though I'd entered a non-digit.
This issue is covered in the FAQ.

<URL:http://jibbering.com/faq/>
I think maybe this whole problem could be solved by using regular
expressions instead of isNaN. I read some dox on RegEx, but didn't
understand much of it. Please help.


The URI below might be of some help. If you need specific help, don't
hesitate to ask.

<URL:http://www.merlyn.demon.co.uk/js-valid.htm>

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #3
Jeff Sandler wrote:
I have a web page. It uses JavaScript to test the user's input
before sending it to the server. It frequently tests using isNaN() with
some very interesting results.

The statements in question test values from text boxes using isNaN() and
also checking if they are still the same after parseInt or parseFloat
are performed on them. The second test is because isNaN() will return
false (is a number) for the string "3a".
You should test isNaN(entry) before you try to parseInt(entry,radix), if
at all.
You need to supply the radix (10, for decimal numbers.)
077 is interpreted as base 8, for example.
You will be better served using a regex to verify a Number, or you could
test an assignment:
if(entry*1){//entry is a Number}

Mick

The curious behavior is that when I enter the string "07" (or any value
less), it returns a number as it should. If I enter the string "08" or
"09", it acts as though I'd entered a non-digit.

I think maybe this whole problem could be solved by using regular
expressions instead of isNaN. I read some dox on RegEx, but didn't
understand much of it. Please help.

To see it, go to
http://ffej2ffej.com/re/start.php

log on as driver 236 with the password 236 then insert new data. Try
"07" and "08" in any of the text boxes and you'll see what I mean when
you try to save. Of course you can see all the javascript by viewing
the source of the frame.

Jul 23 '05 #4
JRS: In article <YL********************@twister.nyroc.rr.com>, dated
Mon, 30 Aug 2004 20:05:12, seen in news:comp.lang.javascript, Mick White
<mw******@BOGUSrochester.rr.com> posted :
or you could
test an assignment:
if(entry*1){//entry is a Number}


Provided that zero is not a valid entry.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Jul 23 '05 #5
Dr John Stockton wrote:
JRS: In article <YL********************@twister.nyroc.rr.com>, dated
Mon, 30 Aug 2004 20:05:12, seen in news:comp.lang.javascript, Mick White
<mw******@BOGUSrochester.rr.com> posted :

or you could
test an assignment:
if(entry*1){//entry is a Number}

Provided that zero is not a valid entry.

I never did quite grasp the concept of zero....
Yes, you make a good point, although in this case the assigment test
suffices.
Mick
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

26 posts views Thread by Chris Potter | last post: by
1 post views Thread by Alex Chan | last post: by
21 posts views Thread by Owen Zhang | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.