Web Analytics Made Easy -
StatCounter I can't check for a NaN value - CodingForum

Announcement

Collapse
No announcement yet.

I can't check for a NaN value

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    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; Sep 4, 2011, 02: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.

    Comment

    Working...
    X