homepage Welcome to WebmasterWorld Guest from 54.161.214.221
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
isNaN fails
LinuxGold




msg:1473579
 4:13 pm on Nov 4, 2005 (gmt 0)

This code fails to detect 1.a as error.

if(isNaN(dhrs))
{
alert("Hours must be numeric")
form.elements.dehrs.focus()
return false;
}

 

Scally_Ally




msg:1473580
 5:02 pm on Nov 4, 2005 (gmt 0)

if(isNaN(parseFloat(dhrs)))
?

maybe?

LinuxGold




msg:1473581
 6:10 pm on Nov 4, 2005 (gmt 0)

This code is where isNaN failed.

function validatedeproc(form)
{
if(form.elements.DE_Cat.options[0].selected)
{
alert("Category is not selected")
form.elements.DE_Cat.focus()
return false;
}
var dqty=form.elements.deqty.value
var dhrs=form.elements.dehrs.value
dqlen=dqty.length
dhlen=dhrs.length
dqty=parseFloat(dqty)
dhrs=parseFloat(dhrs)
if(dqlen==0)
{
alert("Must indicate qty, if nothing, use 0.")
form.elements.deqty.focus()
return false;
}
else
{
if(isNaN(dqty))
{
alert("QTY must be numeric")
form.elements.deqty.focus()
return false;
}
}
if(dhlen==0)
{
alert("No hours entered, please enter hours.")
form.elements.dehrs.focus()
return false;
}
else
{
if(isNaN(dhrs))
{
alert("Hours must be numeric")
form.elements.dehrs.focus()
return false;
}
}
}

Fotiman




msg:1473582
 6:27 pm on Nov 4, 2005 (gmt 0)

You are parsing out the float value.

If you do this:

var n = parseFloat("1.a");

The result is that n equals 1

If you do this:

var n = parseFloat("1.01");

The result is that n equals 1.01

If you remove the parseFloat() call, then your test for isNaN will check against the full value entered (1.a), which will behave the way you want it to.

Hope that helps.

LinuxGold




msg:1473583
 6:33 pm on Nov 4, 2005 (gmt 0)

Ok. That works.

*puzzled*

Why do we need parseFloat?

ajkimoto




msg:1473584
 6:33 pm on Nov 4, 2005 (gmt 0)

LinuxGold,

According to devguru.com, the parseFloat() function determines if the first character in the string argument is a number, parses the string from left to right until it reaches the end of the number, discards any characters that occur after the end of the number, and finally returns the number as a number (not as a string).

This means that if '1.a' is passed to parseFloat(), it will return 1 as a number, discarding the rest of the string.

Why do we need parseFloat?

You still need parseFloat() to turn a string of numerals into a number--like if you had a number that you wanted to extract from the middle of a string.

ajkimoto

Fotiman




msg:1473585
 6:41 pm on Nov 4, 2005 (gmt 0)

parseFloat could be used to do something like this:

var distance = "2.3 miles";
var mydistance = 2 * parseFloat(distance);
alert("You walked " + distance + " but I walked " + mydistance);

For example, to strip off any trailing units.

LinuxGold




msg:1473586
 7:36 pm on Nov 4, 2005 (gmt 0)

*ping*

I got it. Thanks all!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved