Use JS to determine whether the number and decimal combination of the two methods comparison (isNaN and judgment) _javascript skills

Source: Internet
Author: User
The following examples are as follows: If you have a better way, we hope you can put forward a better method.
One: isNaN method:
isNaN () means to be non-numeric, to return False when a number is passed in, and to return true if the pass is not a number
[Empty words are also returned true, a point is returned true, and a number combination returns False, which indicates a number]
Copy Code code as follows:

<script language= "JavaScript" >
function Testnan () {
if (isNaN (' xxx ')) {
Alert (' true ');
}else{
Alert (' false ');
}
}
</script>

-------------------------------Example-----------------------------
<ptml> <pead> <script language= "JavaScript" > Function Isnumber () {var btnobj=document.getelementbyi D ("Idtel"); var Strvalue=btnobj.value; if (!isnan (strvalue)) {alert ("is a number and a few points"); }else{alert ("Please enter a number or number combined with the decimal point!") "); } </script> </pead> <body> <form action= "method=" POST "Name=" "> <input type=" text "NA Me= "Tel" id= "Idtel" value= "" > <input type= "button" Name= "Isnum" value= "Submit" onclick= "Isnumber ()" > </form > </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Two: The method of comparison.
Give an example directly:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" > <HTML> <HEAD> <TITLE> New Document &L t;/title> <meta name= "generator" content= "EditPlus" > <meta name= "Author" content= "" > <meta NAME= "Key Words "content=" "> <meta name=" Description "content=" "> </HEAD> <script language=javascript> func tion Isnumber (String) {var letters = "1234567890.";//You can add your own input value var i,c; if (String.charat (0) = = '-' | | String.charat (string.length-1) = = '-') {return false; for (i = 0; i < string.length i + +) {c = String.charat (i); if (Letters.indexof (c) <0) {return false; } return true; function dobtn () {var Btnobj=document.getelementbyid ("Idtest"); var Strvalue=btnobj.value; if (Isnumber (strvalue)) {alert ("yes"); }else{alert ("No"); } </script> <BODY> <form name= "form" > <input type=text name= "test" id= "Idtest" value= "> <input Type=button name= "" Title= "click"Value= "btn" onclick= "dobtn ();" > </form> </BODY> </HTML>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Summary: The 2nd method is correct if you have always entered a decimal point. And the 1th is not. As long as you do not conform to the digital format, he will not let you through.

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.