Why is a date in mm/dd/yyyy format a number?

K

kaeli

Can anyone explain to me why
01/10/2003 is a number according to isNaN/parseFloat?

Example code:
<html>
<head>
<title> New Document </title>
</head>

<body>
<script>
function isNumber(str)
{
if (isNaN(parseFloat(str))) alert("not a number");
else alert("number");
}
</script>
<form name="f1" onSubmit="return false">
Text: <input type="text" name="t1"><br>
<input type="button" onClick="isNumber(this.form.t1.value)" value="is
number?">
</body>
</html>

--
 
L

Lee

kaeli said:
Can anyone explain to me why
01/10/2003 is a number according to isNaN/parseFloat?


<http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/toplev.html#1064132>

parseFloat parses its argument, a string, and returns a floating point number.
If it encounters a character other than a sign (+ or -), numeral (0-9), a
decimal point, or an exponent, it returns the value up to that point and
ignores that character and all succeeding characters.

It only returns NaN if the first character cannot be converted to a number.
 
L

Laurent Bugnion, GalaSoft

Hi,
Can anyone explain to me why
01/10/2003 is a number according to isNaN/parseFloat?

Example code:

<snipped>

If you alert the result of the parseFloat operation, you'll see that the
number is parsed until the parser finds the first character which
doesn't make sense. In this case, you'll get 1.

This behaviour is consistent in all browsers I know. I don't find this a
bad thing, as long as you know that it's so.

HTH,

Laurent
 
K

kaeli

(thanks to both respondents)
If you alert the result of the parseFloat operation, you'll see that the
number is parsed until the parser finds the first character which
doesn't make sense. In this case, you'll get 1.

This behaviour is consistent in all browsers I know. I don't find this a
bad thing, as long as you know that it's so.

Well, I think it's a bad thing, personally, because something like an
address (123 Blue Ave) would then verify as a number, which it is not.
Phone numbers would also be numbers, which they really aren't, since
adding them makes no sense.
But, since now I know, I can code around it.

Thanks!

--
--
~kaeli~
To steal ideas from one person is plagiarism; to steal from
many is research.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
 
L

Lasse Reichstein Nielsen

[parseFloat only parsing inital part of string]
Well, I think it's a bad thing, personally, because something like an
address (123 Blue Ave) would then verify as a number,

But parseFloat is not meant for verification. It is meant to parse the
inital part of a string to a number (probably intended for use in
parsing input, but not very useful for that, since it doesn't tell
how many characters it matched).

If you just want to convert the entire string to a number, use the
Number function. It gives NaN if the string is not a valid numeral.

The Number function works exactly the same as the internal type
conversion of Javascript.

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top