JS determines whether the input is a number

Source: Internet
Author: User

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.

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.