isNaN function
isNaN (expression:object): Boolean
Evaluates the argument and returns True if the value is NaN (non-numeric). This function can be used to check whether a mathematical expression was successfully evaluated as a number.
The isNaN function is as follows:
The code is as follows |
Copy Code |
function Checknum (str) { if (isNaN (str)) { Alert ("Not a number"); } } |
See Also
The code is as follows |
Copy Code |
parseint ("Hello");//return NAN parseint ("//return"); 110 |
So the simple way to judge whether a string is a number is to take advantage of isNaN (), and if true, the string is not a number, otherwise it is a number
The code is as follows |
Copy Code |
isNaN (parseint ("Hello"));//true; isNaN ("parseint");//false; |
Note: The following methods cannot be used to determine:
The code is as follows |
Copy Code |
parseint ("Hello", ten) ==nan;//return false parseint ("==nan;//return") false |
In addition, you can use regular expressions, the most complete and most accurate method:
The code is as follows |
Copy Code |
function Isnumber (onum) { if (!onum) return false; var strp=/^d+ (. d+)? $/; if (!strp.test (Onum)) return false; try{ if (parsefloat (onum)!=onum) return false; } catch (ex) { return false; } return true; }
|
This JavaScript code is very concise, you can see.
The code is as follows |
Copy Code |
<script language=javascript> function Onlynum () { if (!) ( (event.keycode>=48&&event.keycode<=57) | | (event.keycode>=96&&event.keycode<=105))) Consider the number keys on the keypad Event.returnvalue=false; } </script> <input onkeydown= "Onlynum ();" >
|
There is a more stupid way, we can also see, study
The next time you stick with a regular validation number
The code is as follows |
Copy Code |
<script language= "JavaScript" > function Checkmyform () { var txt = myform.mytext.value; if (CheckNumber (TXT)) { Alert ("Only allow numbers to be entered!") "); return false; } return true; } function CheckNumber (String) { var letters = "1234567890"; var i; var C; for (i = 0; i < string.length i + +) { c = String.charat (i); if (Letters.indexof (c) ==-1) { return true; } } return false; } </script> <body> <form name= "MyForm" onsubmit= "return Checkmyform ()" > <input type= "text" name= "MyText" > <input type= "Submit" Name= "Mysubmit" > </form> </body> |
The simplest JavaScript validation is not a number code as follows:
The code is as follows |
Copy Code |
<input type= ' TEXT ' onkeypress= ' if (event.keycode==13) {if (isNaN (this.value)) alert (' Not numeric ')}; > |
Regular numeric expressions used in common use
1)
The code is as follows |
Copy Code |
"^\d+$"//non-negative Integer (positive integer + 0) "^[0-9]*[1-9][0-9]*$"//Positive integer "^ ((-\d+) | (0+)) $ "//non-positive integer (negative integer + 0) "^-[0-9]*[1-9][0-9]*$"//Negative integer "^-?\d+$"//Integer "^\d+ ()//nonnegative floating-point number (positive floating-point number + 0) "^ ([0-9]+\. [0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*\. [0-9]+) | ([0-9]*[1-9][0-9]*)] $ "//Positive floating-point number "^ ((-\d+ ()//non-positive floating-point number (negative floating-point number + 0) ^ (-([0-9]+\. [0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*\. [0-9]+) | ([0-9]*[1-9][0-9]*))] $ "//negative floating-point number "^ (-?\d+) ("//floating-point number |
2)
code is as follows |
copy code |
var r =/^+?[ 1-9][0-9]*$/; Positive integer R.test (str); |