< turn > Common regular Expression Daquan, mobile phone, phone, email, ID card (the most stringent authentication), IP address, URL, date, etc., the general front of the JS verification

Source: Internet
Author: User

< script type = "Text/javascript" >/** Mobile Phone number format * Only allow numbers starting with 13, 15, 18 * For example: 13012345678, 15929224344, 18201234676*/varRegmobile =/^1[3,5,8]\d{9}$/;/** Fixed telephone number format * Because the fixed phone format is more complex, the situation is more, mainly verify the following types of * such as: 010-12345678, 0912-1234567, (010)-12345678, (0912) 1234567, (010) 12345678, (0912)-1234567, 01012345678, 09121234567*/varRegphone =/^ (^0\d{2}-?\d{8}$) | (^0\d{3}-?\d{7}$) | (^0\d2-?\d{8}$) | (^0\d3-?\d{7}$) $/;/** Email address * such as: [email protected], [email protected], [email protected]*/varRegemail =/^ ([a-za-z0-9]+[_|\-|\.]?) *[a-za-z0-9][email protected] ([a-za-z0-9]+[_|\-|\.]?) *[a-za-z0-9]+ (\.[ a-za-z]{2,3}) +$/;/** ID card 15-bit encoding rules: DDDDDD YYMMDD xx p * Dddddd:6 region code * YYMMDD: Year of birth (two years) month day, such as: 910215 * XX: Sequential encoding, System generation, unable to determine * p: gender, odd male, even female  * * ID card 18-bit encoding rules: dddddd yyyymmdd xxx y * dddddd:6 region code * YYYYMMDD: Year of birth (four years) month day, such as: 19910215 * XXX: Sequential encoding, System generation, cannot be determined, odd for male, even for female * y:  Check code, this bit value can be calculated by the first 17 digits * * * First 17 digits weighting factor for Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2] * Verify bit Y = [1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2] * If the verification code is exactly 10, in order to ensure that the ID is 18, then the 18th bit will use X instead of the * check digit calculation formula: y_p = mod (∑ (AIXWI), one) * I for ID card number 1 ... 17 bits; Y_p is the location of the check code array where the checksum y is located*/functionValidateidcard (idcard) {//Regular expressions for 15-bit and 18-bit ID numbers    varRegidcard =/^ (^[1-9]\d{7} ((0\d) | ( 1[0-2]) (([0|1|2]\d) |3[0-1]) \d{3}$) | (^[1-9]\d{5}[1-9]\d{3} ((0\d) | ( 1[0-2]) (([0|1|2]\d) |3[0-1]) ((\d{4}) |\d{3}[xx]) $) $/; //If you pass this verification, the ID card is in the correct format, but the accuracy needs to be calculated    if(Regidcard.test (Idcard)) {if(Idcard.length = = 18) {            varIdcardwi =NewArray (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);//Save the first 17-bit weighting factor in an array            varIdcardy =NewArray (1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2);//This is the 11-bit remainder, the verification code, which is divided by 11, which can also be saved as an array            varidcardwisum = 0;//used to hold the sum of the first 17 digits after the weighted factor.             for(vari = 0; I < 17; i++) {idcardwisum+ = Idcard.substring (i, i + 1) *Idcardwi[i]; }            varIdcardmod = idcardwisum% 11;//calculates the position of the array where the checksum is located            varIdcardlast = idcard.substring (17);//get the last ID number .            //if it equals 2, then the check code is 10, the last digit of the ID number should be x            if(Idcardmod = = 2) {                if(Idcardlast = = "X" | | idcardlast = = "X") {alert ("Congratulations passed the verification!" "); } Else{alert ("Wrong ID number!" "); }            } Else {                //use the calculated verification code to match the last identity card number, if the same, the explanation passed, otherwise it is invalid ID number                if(Idcardlast = =Idcardy[idcardmod]) {Alert ("Congratulations passed the verification!" "); } Else{alert ("Wrong ID number!" "); }            }        }    } Else{alert ("ID card format is incorrect!"); }}/** can only be a positive integer*/varRegNum =/^\d+$/;/** ZIP Code*/varRegpostcode =/^\d{6}$/;/** User name * can only be alphanumeric underline, and start with a letter (5-16 bit)*/varRegusername =/^[a-za-z]\w{4,15}$/;/** IP address * such as: 192.168.1.102*/varREGIP =/^ (([1-9]\d?) | (1\d{2}) | (2[0-4]\d) | (25[0-5])) \.) {3} ([1-9]\d?) | (1\d{2}) | (2[0-4]\d) | (25[0-5])) $/;/** can only be Chinese characters*/varRegchinesechar =/^[\u4e00-\u9fa5]+$/;/** URL * Only allow HTTP, HTTPS, FTP these three kinds of * such as: http://www.baidu.com*/varRegweb =/^ ([hh][tt]{2}[pp][ss]?) | ([ff][tt][pp])) \:\/\/[ww]{3}\. [\w-]+\.\w{2,4} (\/.*)? $/;/** Date Format verification * Because the date format is more, the main validation of the following types * 2012-05-14, 2012/05/6, 2012.5.14, 20120528*/varRegDate =/^[1-9]\d{3} ([-|\/|\.])? ((0\d) | ([1-9]) | (1[0-2])) \1 (([0|1|2]\d) | ( [1-9]) | 3[0-1]) $/;/** Call the above regular expression method * To verify the phone number format as an example*/functionOnCheck (tel) {if(Regmobile.test (tel)) {Alert ("Congratulations passed the verification!" "); } Else{alert ("Not properly formatted!" "); }}</script>

< turn > Common regular Expression Daquan, mobile phone, phone, email, ID card (the most stringent authentication), IP address, URL, date, etc., the general front of the JS verification

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.