Common regular Expressions 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 numbers starting with 13, 15 and 18 are allowed.
* such as: 13012345678, 15929224344, 18201234676
*/
var regmobile=/^1[3,5,8]\d{9}$/;

/*
* Fixed phone number format
* Because the fixed phone format is more complex, the situation is more, the main verification of the following types
* such as: 010-12345678, 0912-1234567, (010)-12345678, (0912) 1234567, (010) 12345678, (0912)-1234567, 01012345678, 09121234567
*/
var regphone=/^ (^0\d{2}-?\d{8}$) | (^0\d{3}-?\d{7}$) | (^< Span id= "mathjax-span-2" class= "Mrow" >0 \d2 -?\d{8}$) | (^0\d 3 -?\d{7}$) $/;

/*
 * email address
 * such as: [email protected] , Span class= "__cf_email__" data-cfemail= "721e1b5f011b324041445c1c1706" >[email protected] , [email protected]
 */
var regemail=/^ ([a-za-z0-9]+[_|\-|\.]?) *[a-za-z0-9][email protected] ([a-za-z0-9]+[_|\-|\.]?) *[a-za-z0-9]+ (\.[ a-za-z]{2,3}) +$/;

/*
 * ID 15-bit encoding rules: DDDDDD YYMMDD xx p
 * dddddd:6 bit area code
 * YYMMDD: Year of Birth (two year) month day, such as: 910215
 * XX: Sequential encoding, System generation, unable to determine
 * P: Gender, odd male, even female
 * 
 * ID 18-bit encoding rule: dddddd yyyymmdd xxx y
 * dddddd:6 Area code
 * YYYYMMDD: Year of birth (four-year) month day, such as: 19910215
 * XXX: Sequential encoding, System generation, indeterminate, odd male, even female
  * Y: Check code, this bit value can be calculated by the first 17 bits of the
 * 
 * the first 17 digits of the weighted factor are Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
 * Verify bit Y = [1, 0, 9, 8, 7, 6, 5, 4, 3, 2]
 * If the verification code is exactly 10, in order to ensure that the ID is 18 bits, the 18th bit will replace the
 * check bit meter with X Calculation formula: y_p = mod (∑ (AIXWI), one)
 * i is the ID card number 1 ... 17 bits; Y_p is a regular expression of the checksum array location
 */
Function Validateidcard (idcard) {
&NBSP;//15 bit and 18-bit ID number for the check code y
 var regidcard=/^ (^[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) {
var idcardwi=new Array (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
var idcardy=new Array (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
var idcardwisum=0; Used to hold the sum of the first 17 digits after the weighted factor.
for (Var i=0;i<17;i++) {
Idcardwisum+=idcard.substring (i,i+1) *idcardwi[i];
}

Var idcardmod=idcardwisum%11;//calculates the position of the array where the checksum is located
var idcardlast=idcard.substring (17);//Get 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 are verified!") ");
}else{
Alert ("ID card number is wrong! ");
}
}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 are verified!") ");
}else{
Alert ("ID card number is wrong! ");
}
}
}
}else{
Alert ("ID card format is incorrect!");
}
}

/*
* Can only be a positive integer
*/
var regnum=/^\d+$/;

/*
* ZIP Code
*/
var regpostcode=/^\d{6}$/;

/*
* User Name
* Can only be alphanumeric underline, and start with a letter (5-16 bit)
*/
var regusername=/^[a-za-z]\w{4,15}$/;

/*
* IP Address
* such as: 192.168.1.102
*/
var regip=/^ (([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
*/
var regchinesechar=/^[\u4e00-\u9fa5]+$/;

/*
* URL
* Only three kinds of HTTP, HTTPS, FTP are allowed
* such as: http://www.baidu.com
*/
var regweb=/^ ([Hh][tt]{2}[pp][ss]?) | ([ff][tt][pp])) \:\/\/[ww]{3}\. [\w-]+\.\w{2,4} (\/.*)? $/;

/*
* Date Format Verification
* Because the date format is more, the following types are mainly verified
* 2012-05-14, 2012/05/6, 2012.5.14, 20120528
*/
var regdate=/^[1-9]\d{3} ([-|\/|\.])? ((0\d) | ([1-9]) | (1[0-2])) \1 (([0|1|2]\d) | ( [1-9]) | 3[0-1]) $/;

/*
* Methods to invoke the above regular expressions
* To verify the phone number format as an example
*/
function OnCheck (tel) {
if (Regmobile.test (tel)) {
Alert ("Congratulations are verified!") ");

}else{
Alert ("Not formatted correctly! ");
}
}
</script>

Common regular Expressions 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.