JS determines whether the input is a number
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title>js determine if input is digital </title>
<script language= "JavaScript Tutorial" >
function Ischecknum ()
{
var num = document.getElementById (' Isnum '). Value;
if (num)
{
if (!isnan (num))
{
Alert (' is a number ');
return false;
}
Else
{
Alert (' The data you enter is not a number ');
Myfm.isnum.select ();
return false;
}
}
Else
{
Alert (' input content ');
Myfm.isnum.focus ();
}
}
</script>
<body>
<form name= "myFM" method= "POST" action= "" >
<label>
<input type= "text" name= "Isnum" id= "Isnum" >
</label>
<label>
<input type= "button" Name= "Submit" value= "to detect whether the number" onclick= "ischecknum (); >
</label>
</form>
</body>
Note: In JavaScript to determine whether the user input content is a number we just use isNaN to judge the OK.
isNaN (NumValue) The required option NumValue parameter is the value to check for NAN
If the value is NaN, then the isNaN function returns True, otherwise it returns false
The typical case for using this function is to check the return value of the parseint and Parsefloat methods. There is also a way in which a variable can be compared with itself. If the results of comparisons are unequal, then it is NaN. This is because NaN is the only value that is unequal to itself.