Summary of methods for determining whether a string contains numbers and special characters using JS
This article mainly introduces how to use JavaScript to determine whether a string contains numbers and special characters. There are several different methods in this article, including common JS verification method and regular expression method, in addition, there are js functions that determine whether the function is a floating point number. At last, we will briefly introduce how to use the isNAN Function and Its examples. Let's study it together.
1. Determine whether a regular expression is a number, including a positive integer:
Function checkRate (input) {var re =/^ [0-9] + .? [0-9] * $/; // determines whether the string is a number. // If the string is a positive integer, the following is: /^ [1-9] + [0-9] * $/if (! Re. test (input. rate. value) {alert ("enter a number (for example, 0.02)"); input. rate. focus (); return false ;}}
Ii. Common JS Function Methods:
function BASEisNotNum(theNum){if (BASEtrim(theNum)=="")return true;for(var i=0;i
"9")return true;}return false;}
3. determine whether it is a positive number, that is, a positive integer:
function BASEisNotInt(theInt){theInt=BASEtrim(theInt);if ((theInt.length>1 && theInt.substring(0,1)=="0") || BASEisNotNum(theInt)){return true;}return false;}
4. Determine whether a string is composed of numbers and other symbols, such as "-":
function ismonth(str){for(ilen=0;ilen
'9' ){if((str.charAt(ilen)!='-'))return false;}}return true;}
5. Determine if it is a floating point number:
function BASEisNotFloat(theFloat){len=theFloat.length;dotNum=0;if (len==0)return true;for(var i=0;i
"9") && oneNum!=".") || dotNum>1)return true;}if (len>1 && theFloat.substring(0,1)=="0"){if (theFloat.substring(1,2)!=".")return true;}return false;
About javascript isNaN function: usage rule: isNaN (expression: Object): Boolean. Calculate the parameter. If the value is NaN (not a number), true is returned. This function can be used to check whether a mathematical expression is successfully calculated as a number.
Availability: Flash Player 5; ActionScript 1.0; parameter expression: Object-Boolean value, variable, or other expressions to be calculated. Returns a Boolean-Boolean value.
This is often used in submitting forms:
Script if (isNaN (document. login. imgcode. value) {alert ('verification code is not a number! ') Document. login. imgcode. focus (); return false;} script