Welcome to WebmasterWorld Guest from 54.147.250.33

Forum Moderators: open

isNaN fails

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

10+ Year Member



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)

5+ Year Member



if(isNaN(parseFloat(dhrs)))
?

maybe?

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

10+ Year Member



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)

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



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)

10+ Year Member



Ok. That works.

*puzzled*

Why do we need parseFloat?

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

10+ Year Member



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)

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



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)

10+ Year Member



*ping*

I got it. Thanks all!

 

Featured Threads

Hot Threads This Week

Hot Threads This Month