Results 1 to 5 of 5

Thread: isNan

  1. #1
    Join Date
    Nov 2007
    Posts
    151
    Thanks
    67
    Thanked 0 Times in 0 Posts

    Default isNan

    Hi,

    I try to check if this text field include a number or not.

    Where did I go wrong?

    PHP Code:
    <form id="form1" name="form1" method="post" action="">
      
    Your Number: <input name="input" type="text" id="input" value="0" />
      <
    br />
      <
    input name="button" type="submit" id="button" onclick="if (isNanN(document.getElementById('input'))) alert('Please Enter a Valid Number');" value="Submit" />
    </
    form

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Code:
    onclick="if (isNanN
    should be

    Code:
    onclick="if (isNaN
    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #3
    Join Date
    Jul 2008
    Posts
    128
    Thanks
    0
    Thanked 17 Times in 16 Posts

    Default

    Aside from the syntax error mentioned, you must read the .value property.
    That is a weak test since isNaN does not return true for "" or " ". You must test first for the presence of at least one digit:
    Code:
    onclick="if( !/\d/.test( document.getElementById('input').value ) || isNaN(document.getElementById('input').value) ) alert('Please Enter a Valid Number');"

  4. The Following 2 Users Say Thank You to clueful For This Useful Post:

    d-machine (08-23-2009),thetestingsite (08-23-2009)

  5. #4
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    This is very true. Thanks for adding that clueful.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  6. The Following User Says Thank You to thetestingsite For This Useful Post:

    d-machine (08-23-2009)

  7. #5
    Join Date
    Nov 2007
    Posts
    151
    Thanks
    67
    Thanked 0 Times in 0 Posts

    Default

    Thank you both very much!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •