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.