Summary of how to use JS to determine whether a string contains numbers and special characters

Source: Internet
Author: User
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
Related Article

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.