Common Regular Expressions in JS and regular expressions for verification time, and regular expressions for js Verification

Source: Internet
Author: User
Tags email account

Common Regular Expressions in JS and regular expressions for verification time, and regular expressions for js Verification

In this article, I have compiled 12 extremely useful regular expressions, which are a favorite of WEB developers.

1. You can only enter the amount in the input box. In fact, you can only enter numbers with a maximum of two decimal places. // the first type is restricted in the input box.

<Input type = "text" maxlength = "8" class = "form-control" id = "amount" style = "margin-right: 2px; "value =" "onChange =" count (); "onkeyup =" if (this. value = this. value2) return; if (this. value. search (/^ \ d *(? : \. \ D {0, 2 })? $/) =-1) this. value = (this. value2 )? This. value2: ''; else this. value2 = this. value;"> meta // The second method for dynamically adding forms can only be verified in the js method. Var amount = $ ("# amount"). val (); if (amount. search (/^ \ d *(? : \. \ D {0, 2 })? $/) =-1) {alert ("the amount format is incorrect. A maximum of two decimal places are allowed."); return false ;}

2. Verify the email format

Var reg =/\ w + ([-+.] \ w +) * @ \ w + ([-.] \ w + )*\. \ w + ([-.] \ w +) */; var email = $ ("# email "). val (); if (! Reg. test (email) {alert ("enter a qualified email account! "); Return false ;}

3. The password must contain 8 to 20 digits, letters, and special characters.

function validatePwd(str) {if (/^.*?[\d]+.*$/.test(str) && /^.*?[A-Za-z]/.test(str)&& /^.*?[~/`!@#$%^&*()_+|{}?;:><\-\]\\[\/].*$/.test(str) && /^.{8,20}$/.test(str)) {return true;}return false;}

4. Verify the phone number

/*** Verify the phone number ** @ param phoneValue the phone number to be verified * @ returns match return true mismatch return false */function validatePhone (phoneValue) {phoneValue = valueTrim (phoneValue); var reg =/^ [1] [0-9] {10} $/; return reg. test (phoneValue );}

5. determine whether it is a Chinese character

/*** Determine whether it is a Chinese character ** @ param charValue * The data to be verified * @ returns matching returns true not matched returns false */function isCharacter (charValue) {var reg =/^ [\ u4e00-\ u9fa5] {0,} $/; return reg. test (charValue );}

6. Whether it is a letter: true: Yes, false: No

Function isChar (charValue) {var charPattern =/^ [a-zA-Z] * $/; // whether it is the letter result = charPattern. test (charValue); return result ;}

7. Determine if it is a number

Function isNum (numValue) {var numPattern =/^ \ d * $/; // The regular expression of the number result = numPattern. test (numValue); return result ;}

8. Integer Regular Expression

Function isInt (intValue) {var intPattern =/^ 0 $ | ^ [1-9] \ d * $/; // The integer regular expression result = intPattern. test (intValue); return result ;}

9. Whether it is a letter or a number

Function isCharNum (flagValue) {var flagPattern =/^ [a-zA-Z0-9] * $/; // whether it is a letter and number result = flagPattern. test (flagValue); return result ;}

10. Check the 18-digit ID card number

/*** Check the 18-digit ID card number (the 15-digit number can only be used to check whether the birthday is correct) ** @ param idCardValue * 18-digit ID card number * @ returns matched returns true not matched returns false */function idCardVildate (cid) {var arrExp = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; // weighting factor var arrValid = [1, 0, "X", 9, 8, 7, 6, 5, 4, 3, 2]; // Verification Code var reg =/^ [1-9] \ d {5} [1-9] \ d {3} (0 \ d) | (1 [0-2]) ([0 | 1 | 2] \ d) | 3 [0-1]) \ d {3} ([0-9] | X) $/; if (reg. test (cid) {var sum = 0, idx; for (var I = 0; I <cid. length-1; I ++) {// sum + = parseInt (cid. substr (I, 1), 10) * arrExp [I];} // computing model (fixed algorithm) idx = sum % 11; // check whether 18th is equal to the Verification Code return arrValid [idx] = cid. substr (17, 1 ). toUpperCase () ;}else {return false ;}}

11. Verify the 15-digit ID card number for a valid birthday

Function isvaliditybrihby15idcard (idCard15) {var year = idCard15.substring (6, 8); var month = idCard15.substring (8, 10); var day = idCard15.substring (10, 12 ); var temp_date = new Date (year, parseFloat (month)-1, parseFloat (day); // if you are older than your ID card, you do not need to consider the Millennium worm problem and use getYear () method if (temp_date.getYear ()! = ParseFloat (year) | temp_date.getMonth ()! = ParseFloat (month)-1 | temp_date.getDate ()! = ParseFloat (day) {return false;} else {return true ;}}

12. verify whether the birthday in the 18-digit ID card number is valid

Function isValidityBrithBy18IdCard (idCard18) {var year = idCard18.substring (6, 10); var month = idCard18.substring (10, 12); var day = idCard18.substring (12, 14 ); var temp_date = new Date (year, parseFloat (month)-1, parseFloat (day); // getFullYear () is used to obtain the year to avoid the problem of Millennium insects if (temp_date.getFullYear ()! = ParseFloat (year) | temp_date.getMonth ()! = ParseFloat (month)-1 | temp_date.getDate ()! = ParseFloat (day) {return false;} else {return true ;}}

PS: Regular Expression of js verification time

Verification time format: 09:00:22

var reDateTime = /^(?:19|20)[0-9][0-9]-(?:(?:0[1-9])|(?:1[0-2]))-(?:(?:[0-2][1-9])|(?:[1-3][0-1])) (?:(?:[0-2][0-3])|(?:[0-1][0-9])):[0-5][0-9]:[0-5][0-9]$/;var isDateTime = reDateTime.test('2012-01-31 09:00:22');

The above section describes common JS Regular Expressions and regular expressions for verification time. I hope this will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.