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>
--
--
~kaeli~
A man needs a mistress... just to break the monogamy. http://www.ipwebdesign.net/wildAtHeart http://www.ipwebdesign.net/kaelisSpace 6 1130
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.
Hi,
kaeli wrote: 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
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch
In article <3f********@news.bluewin.ch>, ga*********@bluewin.ch
enlightened us with...
(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
In article <MP************************@nntp.lucent.com>, kaeli
<ti******@NOSPAM.comcast.net> writes: 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!
As written, your function will say that its a number. Its a flaw in the
function, not a flaw in the mechanisms. When you use parseFloat, it does just
that. It starts at the beginning and when it encounters a non-digit, it stops
and returns what it found. Basically what you are asking is "Does it start with
digits"?
function isNumber(str)
{
if (isNaN(parseFloat(str))) alert("not a number");
else alert("number");
}
Any string that starts with digits will pass, any that start with non-digits
(or nothing at all) will fail it.
function isNumber(str)
{
if (isNaN(+str)) alert("not a number");
else alert("number");
}
Using the unary + to attempt to convert it to number would come closer to what
you are describing though. As above, if it contains non-digits then it wont
convert it to a number.
<URL: http://msdn.microsoft.com/library/de...n-us/script56/
html/js56jsmthisfinite.asp />
isFinite(+str)
is an alternative to the function.
--
Randy
JRS: In article <3f********@news.bluewin.ch>, seen in
news:comp.lang.javascript, Laurent Bugnion, GalaSoft <galasoft- LB@bluewin.ch> posted at Tue, 6 Jan 2004 20:40:25 :- kaeli wrote:
Can anyone explain to me why 01/10/2003 is a number according to isNaN/parseFloat?
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.
It is a good thing; there is no more reason to recognise 01/10/2003 as a
date meaning 1st Oct 2003 than to recognise it as a division resulting
in 0.00004992511233150275. The interpretation of a string must depend
on the means chosen to interpret it.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
kaeli <ti******@NOSPAM.comcast.net> writes:
[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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.' This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Macca |
last post by:
Hi,
I have a table which has a date/time field. I am storing them as follows :-
01/01/2005 11:25
01/01/2005 19:44
02/01/2005 05:04
|
by: Assimalyst |
last post by:
Hi,
I have a working script that converts a dd/mm/yyyy text box date entry
to yyyy/mm/dd and compares it to the current date, giving an error
through an asp.net custom validator, it is as...
|
by: Alok yadav |
last post by:
i am using a webservice in which a method is serach. i use this method
which accept a argument of date type in dd/MM/yyyy formate. i have a
textbox which accept the date from the user, when i...
|
by: Petyr David |
last post by:
Just looking for the simplest. right now my perl script returns an
error messge to the user if the date string is invalid. would like to
do this before accessing the server.
TX
|
by: fniles |
last post by:
On my machine in the office I change the computer setting to English (UK) so
the date format is dd/mm/yyyy instead of mm/dd/yyyy for US.
This problem happens in either Access or SQL Server.
In the...
| |
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |