Welcome to WebmasterWorld Guest from 54.234.38.8

Forum Moderators: open

Message Too Old, No Replies

isNaN fails

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

Junior Member

10+ Year Member

joined:Aug 21, 2003
posts:74
votes: 0


This code fails to detect 1.a as error.

if(isNaN(dhrs))
{
alert("Hours must be numeric")
form.elements.dehrs.focus()
return false;
}
5:02 pm on Nov 4, 2005 (gmt 0)

Full Member

10+ Year Member

joined:Aug 3, 2005
posts:269
votes: 0


if(isNaN(parseFloat(dhrs)))
?

maybe?

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

Junior Member

10+ Year Member

joined:Aug 21, 2003
posts:74
votes: 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;
}
}
}
6:27 pm on Nov 4, 2005 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts:4965
votes: 10


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.

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

Junior Member

10+ Year Member

joined:Aug 21, 2003
posts:74
votes: 0


Ok. That works.

*puzzled*

Why do we need parseFloat?

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

Junior Member

10+ Year Member

joined:Jan 8, 2004
posts:162
votes: 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

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

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts:4965
votes: 10


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.

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

Junior Member

10+ Year Member

joined:Aug 21, 2003
posts:74
votes: 0


*ping*

I got it. Thanks all!