JS realization of the second generation of ID card number verification detailed

Source: Internet
Author: User
Tags regular expression

The code is as follows:




/*


according to the national standard GB 11643-1999〗 in accordance with the provisions of the civil Status number, the citizenship number is a feature combination code, by 17-digit ontology code and a digital parity code. The order is from left to right: six digit address code, eight digit birth date code, three digit sequential code and one digit parity code.

The
address code represents the administrative division code of the county (city, Flag, district) of the resident residence of the encoding object.


Birth date Code represents the year, month, and day of the encoding object's birth, where the year is represented by four digits, and the year, month, and day are not separated.

The
sequence code represents the sequence number of persons born in the same same address code, in the range of the area identified by the same one, month and day. The odd number of sequential codes is given to men, even to females.


Check code is based on the front 17 digit code, according to the ISO 7064:1983.mod 11-2 Check code calculated by the inspection code.


 


Birth date calculation method.

The
ID code first expands the birth year to 4 digits, simply by adding a 19 or 18, which includes all the people born in 1800-1999;


years after the birth of the affirmation is 18 without this trouble, as for 1800 years ago, then what should not have ID number this dongdong, ⊙﹏⊙ b khan ...


The following is a regular expression:


Date of birth 1800-2099 (18|19|20) d{2} (0[1-9]|1[12)) (0[1-9]|[ 12]D|3[01])


identity card Regular Expression/^d{6} (18|19|20)? D{2} (0[1-9]|1[12)) (0[1-9]|[ 12]D|3[01]) d{3} (d| X) $/i


bit Check rule 6-bit address code + 6-bit Birth date + 3-bit sequential number


bit Check rule 6-bit address code + 8-bit Birth date + 3-bit sequence number + 1-bit check bit


 


Check bit rule formula: ∑ (AIXWI) (mod 11) ..... .................... ........ ... (1)


formula (1):


I----The ordinal of a number character from the to the left including the check code;


AI----Represents the number character value on position I;


Wi----The weighted factor at position I, whose numerical value is calculated based on the formula wi=2^ (n-1) (mod 11).


I 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1


Wi 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2 1


 


*/


//ID number legality verification


//support for 15-bit and 18-bit ID card number


//Support address encoding, birth date, check bit verification


function Identitycodevalid (code) {


var city={11: "Beijing", 12: "Tianjin", 13: "Hebei", 14: "Shanxi", 15: "Inner Mongolia", 21: "Liaoning", 22: "Jilin", 23: "Heilongjiang", 31: "Shanghai", 32: "Jiangsu", 33: "Zhejiang", 34: "Anhui" , 35: "Fujian", 36: "Jiangxi", 37: "Shandong", 41: "Henan", 42: "Hubei", 43: "Hunan", 44: "Guangdong", 45: "Guangxi", 46: "Hainan", 50: "Chongqing", 51: "Sichuan", 52: "Guizhou", 53: "Yunnan", 54: " Tibet ", 61:" Shaanxi ", 62:" Gansu ", 63:" Qinghai ", 64:" Ningxia ", 65:" Xinjiang ", 71:" Taiwan ", 81:" Hong Kong ", 82:" Macao ", 91:" Foreign "};


var tip = "";


var pass= true;

 if (!code | |!/^d{6} (18|19|20) d{2} (0[1-9]|1[12)) (0[1-9]|[ 12]D|3[01]) d{3} (d| X) $/i.test {
 tip = "Bad ID card format";
Pass = false;
 }

else if (!CITY[CODE.SUBSTR (0,2)]) {
tip = "Address encoding error";
pass = false;
}
else{
18-bit ID card needs to verify the last check digit
if (code.length = = 18) {
Code = Code.split (');
∑ (AIXWI) (mod 11)
Weighting factor
var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
Check digit
var parity = [1, 0, ' X ', 9, 8, 7, 6, 5, 4, 3, 2];
var sum = 0;
var ai = 0;
var wi = 0;
for (var i = 0; i < i++)
{
ai = Code[i];
WI = Factor[i];
Sum + + ai * WI;
}
var last = parity[sum% 11];
if (parity[sum%]!= code[17]) {
tip = "Check digit error";
Pass =false;
}
}
}
if (!pass) alert (TIP);
return pass;
}
var c = ' 130981199312253466 ';
var res= identitycodevalid (c);

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.