Common Javascript Regular Expressions

Source: Internet
Author: User
The input must be verified. The verification is mainly completed by writing regular expressions in JS. The common Regular Expressions collected on the Internet are put here for future use. I would like to pay tribute to the original creators.  
/* Purpose: Check whether the entered email address format is correct. Input: stremail: String return: true if verification is passed; otherwise, false */function checkemail (stremail) is returned) {// var emailreg =/^ [_ a-z0-9] + @ ([_ a-z0-9] + \.) + [a-z0-9] {2, 3} $/; var emailreg =/^ [\ W-] + (\. [\ W-] +) * @ [\ W-] + (\. [\ W-] +) + $/; If (emailreg. test (stremail) {return true;} else {alert ("the email address format you entered is incorrect! "); Return false ;}};/* purpose: Verify the format of the IP address input: Strip: IP address return: If the verification succeeds, return true; otherwise, return false; */function isip (strip) {If (isnull (strip) {return false;} var Re =/^ (\ D + )\. (\ D + )\. (\ D + )\. (\ D +) $/g // The regular expression that matches the IP address if (Re. test (strip) {If (Regexp. $1 <256 & Regexp. $2 <256 & Regexp. $3 <256 & Regexp. $4 <256) {return true ;}} return false ;};/* purpose: Check whether the entered mobile phone number is correct. Input: strmobile: String return: true if verified, otherwise, false */Fu is returned. Nction checkmobile (strmobile) {var regu =/^ [1] [3] [0-9] {9} $/; var Re = new Regexp (regu ); if (Re. test (strmobile) {return true;} else {return false ;};/* purpose: Check whether the entered phone number format is correct. Input: strphone: string and return: if true is returned after verification, otherwise false */function checkphone (strphone) is returned) {var phoneregwitharea =/^ [0] [1-9] {2, 3}-[0-9] {5, 10} $ /; vaR phoneregnoarea =/^ [1-9] {1} [0-9] {5, 8} $/; var prompt = "the phone number you entered is incorrect! "If (strphone. length> 9) {If (phoneregwitharea. test (strphone) {return true;} else {alert (prompt); Return false ;}} else {If (phoneregnoarea. test (strphone) {return true;} else {alert (prompt); Return false ;}}};

/* Purpose: Check whether the input string is null or all are spaces. Input: Str returns: true if all are null; otherwise, false */function isnull (STR) is returned) {If (STR = "") {return true;} var regu = "^ [] + {1} quot; var Re = new Regexp (regu); Return re. test (STR) ;};/* purpose: Check whether the value of the input object complies with the value of the integer format input: Str input string returns: If the verification succeeds, true is returned, otherwise, false */function isinteger (STR) {var regu =/^ [-] {0, 1} [0-9] {1, }$/; return regu. test (STR) ;};/* purpose: Check whether the input string conforms to the positive integer format. Input: S: String returns: If it passes verification, true is returned. Otherwise Return false */function isnumber (s) {var regu = "^ [0-9] + {1} quot; var Re = new Regexp (regu); If (S. search (re )! =-1) {return true;} else {return false ;};/* purpose: Check whether the input string is in decimal number format. It can be a negative number. Input: Str: String returns: if true is returned after verification, otherwise false */function isdecimal (STR) {If (isinteger (STR) {return true;} is returned ;} vaR Re =/^ [-] {0, 1} (\ D +) [\.] + (\ D +) $/; If (Re. test (STR) {If (Regexp. $1 = 0 & Regexp. $2 = 0) {return false;} return true;} else {return false ;}};/* purpose: Check whether the value of the input object complies with the port number format input: return the string entered by STR: if true is returned after verification, otherwise false */function isport (STR) {return (isnumber (STR) & STR <65536);} is returned );}; /* purpose: Check whether the input string conforms to the amount format. The format is defined as a positive number with decimals. Up to three digits after the decimal point are input: S: String return: true if verification is successful, otherwise, false */function ismoney (s) {var regu = "^ [0-9] + [\.] is returned. [0-9] {0, 3} {1} quot; var Re = new Regexp (regu); If (Re. test (s) {return true;} else {return false ;}};

/* Purpose: Check whether the input string is composed of English letters, numbers, and underscores. Input: S: String. Return: if the result is verified, true is returned. Otherwise, false is returned. */function isnumberor_letter (s) is returned) {// determine whether it is a number or letter var regu = "^ [0-9a-za-z \ _] + {1} quot; var Re = new Regexp (regu); If (Re. test (s) {return true;} else {return false ;};/* purpose: Check whether the input string is composed of only English letters and numbers. Input: S: String returns: if true is returned after verification, otherwise false */function isnumberorletter (s) {// determines whether it is a number or letter var regu = "^ [0-9a-za-z] + {1} quot ;; vaR Re = new RegEx P (regu); If (Re. test (s) {return true;} else {return false ;};/* purpose: Check whether the input string consists of only Chinese characters, letters, and numbers. Input: s: string return: if true is returned after verification, otherwise false */function ischinaornumborlett (s) is returned) {// determine whether the component is a var regu = "^ [0-9a-za-z \ u4e00-\ u9fa5] + {1} quot; var Re = new Regexp (regu ); if (Re. test (s) {return true;} else {return false ;}};/* purpose: determine whether it is a date input: Date: date; FMT: Return in date format: if true is returned after verification, otherwise false */function isdate (Date, FMT) {If (FMt = NULL) {FMt = "yyyymmdd";} var yindex = FMT. indexof ("YYYY"); If (yindex =-1) {return false;} var year = date. substring (yindex, yindex + 4); var mindex = FMT. indexof ("mm"); If (mindex =-1) {return false;} var month = date. substring (mindex, mindex + 2); var dindex = FMT. indexof ("DD"); If (dindex =-1) {return false;} var day = date. substring (dindex, dindex + 2 ); If (! Isnumber (year) | year> "2100" | year <"1900") {return false;} If (! Isnumber (month) | month> "12" | month <"01") {return false;} If (day> getmaxday (year, month) | day <"01") {return false;} return true ;}; function getmaxday (year, month) {If (month = 4 | month = 6 | month = 9 | month = 11) {return "30";} If (month = 2) {If (Year % 4 = 0 & year % 100! = 0 | year % 400 = 0) {return "29";} else {return "28";} return "31 ";;}};

/* Purpose: whether character 1 ends with string 2. Input: str1: string; str2: included string. Return: true if verification succeeds, otherwise, false */function islastmatch (str1, str2) {var Index = str1.lastindexof (str2); If (str1.length = index + str2.length) {return true;} return false ;}; /* purpose: whether character 1 starts with string 2. Input: str1: string; str2: included string. Return: true if verification succeeds, otherwise, false */function isfirstmatch (str1, str2) {var Index = str1.indexof (str2); If (Index = 0) {return true;} return Fals E;};/* purpose: character 1 is a string containing 2 input: str1: string; str2: contained string return: true if verification is passed, otherwise, false */function ismatch (str1, str2) {var Index = str1.indexof (str2); If (Index =-1) {return false;} return true;} is returned ;}; /* purpose: Check whether the input start and end dates are correct. The rule is that the format of the two dates is correct and the end date is as scheduled> = Start Date input: startdate: start date, string; enddate: end on schedule, string return: if true is returned through verification, otherwise false */function checktwodate (startdate, enddate) {If (! Isdate (startdate) {alert ("Incorrect start date! "); Return false;} else if (! Isdate (enddate) {alert ("the end date is incorrect! "); Return false;} else if (startdate> enddate) {alert (" the start date cannot be greater than the end date! "); Return false;} return true ;};

/* Purpose: Check the number of selected check boxes input: checkboxid: String return: return the number of selected check boxes */function checkselect (checkboxid) {var check = 0; vaR I = 0; If (document. all (checkboxid ). length> 0) {for (I = 0; I <document. all (checkboxid ). length; I ++) {If (document. all (checkboxid ). item (I ). checked) {check + = 1 ;}} else {If (document. all (checkboxid ). checked) {check = 1 ;}} return check;} function gettotalbytes (varfiel D) {If (varfield = NULL) {return-1;} var totalcount = 0; for (I = 0; I <varfield. value. length; I ++) {If (varfield. value. charcodeat (I)> 127) {totalcount + = 2;} else {totalcount ++;} return totalcount;} function getfirstselectedvalue (checkboxid) {VaR value = NULL; vaR I = 0; If (document. all (checkboxid ). length> 0) {for (I = 0; I <document. all (checkboxid ). length; I ++) {If (Document. all (checkboxid ). item (I ). checked) {value = document. all (checkboxid ). item (I ). value; break ;}}else {If (document. all (checkboxid ). checked) {value = document. all (checkboxid ). value ;}} return value;} function getfirstselectedindex (checkboxid) {VaR value =-2; var I = 0; If (document. all (checkboxid ). length> 0) {for (I = 0; I <document. all (checkboxid ). length; I ++) {If (Document. all (checkboxid ). item (I ). checked) {value = I; break ;}} else {If (document. all (checkboxid ). checked) {value =-1 ;}} return value;} function selectall (checkboxid, status) {If (document. all (checkboxid) = NULL) {return;} If (document. all (checkboxid ). length> 0) {for (I = 0; I <document. all (checkboxid ). length; I ++) {document. all (checkboxid ). item (I ). checked = stat US ;}} else {document. all (checkboxid ). checked = status ;}} function selectinverse (checkboxid) {If (document. all (checkboxid) = NULL) {return;} If (document. all (checkboxid ). length> 0) {for (I = 0; I <document. all (checkboxid ). length; I ++) {document. all (checkboxid ). item (I ). checked =! Document. All (checkboxid). Item (I). Checked ;}} else {document. All (checkboxid). Checked =! Document. All (checkboxid). Checked;} function checkdate (value) {If (value = '') {return true;} If (value. length! = 8 |! Isnumber (value) {return false;} var year = value. substring (0, 4); If (Year> "2100" | year <"1900") {return false;} var month = value. substring (4, 6); If (month> "12" | month <"01") {return false;} var day = value. substring (6, 8); If (day> getmaxday (year, month) | day <"01") {return false;} return true ;};/* purpose: check whether the input start and end dates are correct. The rule is that the two dates are in correct format or are empty and end dates> = Start Date input: startdate: start date, string; endda Te: end date, string return: true if verification is successful, otherwise false */function checkperiod (startdate, enddate) {If (! Checkdate (startdate) {alert ("Incorrect start date! "); Return false;} else if (! Checkdate (enddate) {alert ("the end date is incorrect! "); Return false;} else if (startdate> enddate) {alert (" the start date cannot be greater than the end date! "); Return false;} return true;};/* purpose: Check whether the securities code is entered correctly: seccode: The securities code returns: If the verification succeeds, true is returned, otherwise, false */function checkseccode (seccode) {If (seccode. length! = 6) {alert ("the length of the securities code should be 6 characters"); Return false;} If (! Isnumber (seccode) {alert ("The securities code can only contain numbers"); Return false;} return true ;};/* function: ctrim (sinputstring, itype) Description: the function parameters: itype: 1 = removes spaces on the left of the string; 2 = removes spaces on the left of the string; 0 = removes spaces on the left and right of the string. Return Value: remove the space string */function ctrim (sinputstring, itype) {var stmpstr = ''; var I =-1; if (itype = 0 | itype = 1) {While (stmpstr = '') {++ I; stmpstr = sinputstring. substr (I, 1);} sinputstring = sinputstring. substring (I);} If (itype = 0 | itype = 2) {stmpstr = ''; I = sinputstring. length; while (stmpstr = '') {-- I; stmpstr = sinputstring. substr (I, 1);} sinputstring = sinputstring. substring (0, I + 1);} return sinputstring ;};

 

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.