PDA

View Full Version : isNan



d-machine
08-22-2009, 10:23 PM
Hi,

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

Where did I go wrong?



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

thetestingsite
08-22-2009, 11:26 PM
onclick="if (isNanN


should be



onclick="if (isNaN


Hope this helps.

clueful
08-23-2009, 03:41 AM
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:
onclick="if( !/\d/.test( document.getElementById('input').value ) || isNaN(document.getElementById('input').value) ) alert('Please Enter a Valid Number');"

thetestingsite
08-23-2009, 03:45 AM
This is very true. Thanks for adding that clueful.

d-machine
08-23-2009, 05:58 AM
Thank you both very much!