Js determines whether the input is a number
<Html xmlns = "http://www.bkjia.com/">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> js determines whether the input is a number </title>
<Script language = "javascript tutorial">
Function ischeckNum ()
{
Var num = document. getElementById ('isnum'). value;
If (num)
{
If (! IsNaN (num ))
{
Alert ('Number ');
Return false;
}
Else
{
Alert ('the data you entered is not a number ');
Myfm. isnum. select ();
Return false;
}
}
Else
{
Alert ('enter required content ');
Myfm. isnum. focus ();
}
}
</Script>
</Head>
<Body>
<Form name = "myfm" method = "post" action = "">
<Label>
<Input type = "text" name = "isnum" id = "isnum">
</Label>
<Label>
<Input type = "button" name = "Submit" value = "check whether it is a number" onClick = "ischeckNum ();">
</Label>
</Form>
</Body>
</Html>
Note: In javascript, we only need to use isNaN to determine whether the user input is a number.
In isNaN (numValue), the required numvalue parameter is the value to be checked for NAN.
If the value is NaN, The isNaN function returns true; otherwise, false.
This function is typically used to check the return values of the parseInt and parseFloat methods. Another way is to compare variables with itself. If the comparison result is different, it is NaN. This is because NaN is the only value that is different from itself.