Js determines whether the input is a number

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.