JS realizes the second generation ID card number verification detailed _javascript skill

Source: Internet
Author: User

Less nonsense, directly on the code, comments in the resolution is very clear, here is not BB.

Copy Code code as follows:

/*
According to the standard GB 11643-1999〗 of the People's Republic of China, the citizen identification number is a characteristic combination code, which consists of 17 digit ontology code and a digital check 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.
The Birth date code represents the year, month, and day of the encoding object's birth, where the year is represented by a four-digit number, and the year, month, and day are not separated.
The sequential 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. The odd number of sequential codes is given to men, even to females.
The check code is based on the front 17 digit code, according to the ISO 7064:1983.mod 11-2 check code to calculate the inspection code.

Method of Birth date calculation.
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, that 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 sequence number
Bit-Check rule 6-bit address code + 8-bit Birth date + 3-bit sequence number + 1-bit check digit

Check bit rule formula: ∑ (AIXWI) (mod 11) ..... .................... ........ ... (1)
Formula (1):
I----the number of digits from the to left including the check code;
AI----represents the number character value on position I;
The Wi----shows the weighting factor in position I, whose numerical value is calculated according to 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

*/
Verification of the legality of the ID number
Supports 15-bit and 18-digit ID numbers
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 (code)) {
tip = "Wrong ID number 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);


How, very comprehensive first ID number verification code, and online search of other verification code is not a dimensional, small partners need to take the direct use of it.

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.