Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    May 2004
    Posts
    58
    Thanks
    12
    Thanked 0 Times in 0 Posts

    trapping NaN and Infinity

    How can I check to see if a textbox contains NaN or infinity?

    if (t1.value=='NaN')

    doesn't seem to do the trick

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,309
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    isNaN cannot be tested with the == equality operator as it is not a number.

    Use if (isNAN.t1.value)) { // do something

  • #3
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    isNaN(t1.value), actually.

    If you want to check for infinity as well:

    var parsed = parseFloat(t1.value);

    if (isNaN(parsed) || !isFinite(parsed)) {
    // not a number, or +/- infinity
    }
    else {
    // finite number
    }

  • #4
    New Coder
    Join Date
    May 2004
    Posts
    58
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Thank you for the replies. I'd be pulling my hair out if it weren't for you guys!

  • #5
    Regular Coder
    Join Date
    Mar 2004
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts
    isFinite is sufficient. isNaN is not needed.

    isFinite(Number.NaN) // false.


  •  

    Posting Permissions

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