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 2 of 2
  1. #1
    New Coder
    Join Date
    Aug 2011
    Posts
    46
    Thanks
    9
    Thanked 2 Times in 2 Posts

    I can't check for a NaN value

    I have a text input box that resets to blank when the user enters an alpha character, it also checks for the number 0 as it has to be greater than 0 too... when I submit the form it is blank the variable changes to blank (as I use parseInt to convert it to an integer at a later stage)

    What can I do to check and see if it is NaN value?
    (I've tried isNaN() and it did not do what I wanted it to)

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,029
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Show your code - but remember that submitting the form reloads it so all the input values are erased. And the string value "0" is not the same as the number 0.

    This may assist you:-

    Code:
    <input type = "text" id = "text1" onchange = "checkIt()">
    
    <script type = "text/javascript">
    function checkIt() {
    var t = document.getElementById("text1");
    var val = parseInt(t.value,10);
    if (isNaN(val) || val <=0) {
    alert ("Invalid data - must be a number > 0 ");
    t.value = "";
    setTimeout("document.getElementById('text1').focus()", 25);
    return false;
    }
    }
    </script>
    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 09-04-2011 at 06:38 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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