View Full Version : I can't check for a NaN value

09-03-2011, 04:39 PM
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)

Philip M
09-04-2011, 11:18 AM
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:-

<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.