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

Discussion in 'Javascript' started by kaeli, Jan 6, 2004.

  1. kaeli

    kaeli Guest

    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
     
    kaeli, Jan 6, 2004
    #1
    1. Advertising

  2. kaeli

    Lee Guest

    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.
     
    Lee, Jan 6, 2004
    #2
    1. Advertising

  3. 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
     
    Laurent Bugnion, GalaSoft, Jan 6, 2004
    #3
  4. kaeli

    kaeli Guest

    In article <>,
    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
     
    kaeli, Jan 7, 2004
    #4
  5. kaeli <> 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 -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Jan 7, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chu
    Replies:
    3
    Views:
    4,220
    Shawn
    Aug 15, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,128
    Smokey Grindel
    Dec 2, 2006
  3. Ashraf Ansari

    convert MM/dd/yyyy format into dd/MM/yyyy

    Ashraf Ansari, Aug 30, 2007, in forum: ASP .Net
    Replies:
    4
    Views:
    798
    Alexey Smirnov
    Aug 30, 2007
  4. Gilles Ganault

    Converting DD MM YYYY into YYYY-MM-DD?

    Gilles Ganault, Aug 17, 2009, in forum: Python
    Replies:
    18
    Views:
    1,402
    Mark Tolonen
    Aug 20, 2009
  5. J P Singh
    Replies:
    4
    Views:
    293
    J P Singh
    Feb 10, 2004
Loading...

Share This Page