Commonly used regular expression encyclopedia, mobile phone, telephone, mailbox, ID card (the most stringent verification), IP address, URL, date, etc., the general front desk JS verification, here is enough ... __ database

Source: Internet
Author: User

<script type= "Text/javascript" >
/*
* Mobile Phone number format
* Only numbers starting with 13, 15, 18 are allowed
* such as: 13012345678, 15929224344, 18201234676
*/
var regmobile=/^1[3,5,8]\d{9}$/;

/*
* Fixed phone number format
* Because the fixed telephone format is more complex, the situation is more, the main validation 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}$) | (^\ (0\d{2}\)-?\d{8}$) | (^\ (0\d{3}\)-?\d{7}$) $/;

/*
* Email Email
* such as: zhangsan@163.com, Li-si@236.net, Wan_gwu999@SEED.NET.TW
*/
var regemail=/^ ([a-za-z0-9]+[_|\-|\.]?) *[a-za-z0-9]+@ ([a-za-z0-9]+[_|\-|\.]?) *[a-za-z0-9]+ (\.[ a-za-z]{2,3}) +$/;

/*
 * ID 15-bit encoding rule: dddddd yymmdd xx p
 * dddddd:6 bit region code
 * YYMMDD: Year of birth (two-year) month, such as: 910215
&NB sp;* XX: Sequential encoding, System generation, unable to determine
 * p: Sex, odd number for male, even number as female
 *
 * ID 18-bit encoding rule: dddddd yyyymmdd xxx y
 * Dddddd:6-bit region code
 * YYYYMMDD: Year of birth (four-year) month, such as: 19910215
 * XXX: Sequential encoding, System generation, cannot be determined, odd for men, even for women
 * y: Parity code, The bit value can be computed with the first 17 digits to obtain the
 *
 * The first 17 digits weighted factor is Wi = [7, 9, 5, 8, 4, 2, 1, 6, 3, 7, 9, 5, 8, 4,]
&nbs p;* verification bit Y = [1, 0, 9, 8, 7, 6, 5, 4, 3, 2]
 * If the verification code happens to be 10, in order to ensure that the ID card is 18, then the 18th bit will be replaced with x
 * check bit formula: Y_p = MoD (∑ (AIXWI), one)
 * I for ID number 1 ... 17 bits; Y_p is the checksum code y where the checksum code array position
 */
Function Validateidcard (idcard) {
&NBSP;//15 bit and 18-bit ID number regular expression
 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 validation, the ID card format is correct, 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 divided by 11, the possible resulting 11-bit remainder, the CAPTCHA, and the number of groups
var idcardwisum=0; Used to preserve the sum of the first 17 of their well-behaved weighted factors.
for (Var i=0;i<17;i++) {
Idcardwisum+=idcard.substring (i,i+1) *idcardwi[i];
}

Var idcardmod=idcardwisum%11;//calculates the location of the array where the checksum code is located
var idcardlast=idcard.substring (17);/Get the last ID number

If equal to 2, then the check code is 10, the last one of the ID number should be X
if (idcardmod==2) {
if (idcardlast== "X" | | idcardlast== "X") {
Alert ("Congratulations passed the test.") ");
}else{
Alert ("Wrong ID number.") ");
}
}else{
Match the calculated verification code with the last ID number, if consistent, the indication is passed, otherwise the invalid ID number
if (Idcardlast==idcardy[idcardmod]) {
Alert ("Congratulations passed the test.") ");
}else{
Alert ("Wrong ID number.") ");
}
}
}
}else{
Alert ("ID card format is not correct!");
}
}

/*
* Only a positive integer
*/
var regnum=/^\d+$/;

/*
* Postal 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]+$/;

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

/*
* Date Format validation
* Because the date format is more, the main validation of the following types
* 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]) $/;

/*
 * the method of calling the above regular expression
 * to verify the phone number format as an example
 */
Function Oncheck (tel) {
 if ( Regmobile.test (tel)) {
  alert ("Congratulations on verification.") ");
  
 }else{
  alert ("Malformed.") ");
 }
}
</script>

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.