When we are building an Internet website, we often use our ID number when registering personal information. We need to verify our ID card, otherwise others will pass it by simply entering a number, which makes you feel that this website is very shit-made.
There are rules for ID number.
Structure and form
1. The structure of the number
The citizenship number is a feature combination code, consisting of a seventeen-digit ontology code and a one-digit verification code. The arrangement order is from left to right: six-digit address code, eight-digit date of birth code, three-digit sequence code and one-digit verification code.
2. Address code
The administrative division code of the county (city, banner, district) where the permanent residence of the coding object is located shall be implemented in accordance with the provisions of GB/T2260.
3. Date of birth code
It indicates the year, month and day of the birth of the encoded object. It shall be executed according to the provisions of GB/T7408. There is no separator between the year, month and day codes.
4. Sequence code
It indicates that within the area identified by the same address code, the sequence number assigned to people born in the same year, month, and day. The odd number of the sequence code is assigned to men and even numbers are assigned to women.
5. Verification code
According to the first seventeen digit numerical codes, the verification code calculated according to the ISO 7064:1983.MOD 11-2 verification code.
Calculation method
1. Multiply the 17-digit number of the previous ID number by different coefficients. The coefficients from the first position to the seventeenth position are: 7-9-10-5-8-4-2-1-6-3-7-9-10-5-8-4-2.
2. Add the result of multiplying these 17-digit numbers and coefficients.
3. Use the addition and divide by 11 to see what the remainder is?
4. The remainder may only have 11 numbers: 0-1-2-3-4-5-6-7-8-9-10. The number of the last ID card corresponding to it is 1-0-X-9-8-7-6-5-4-3-2.
5. From the above, you know that if the remainder is 3, the 18th digit of the ID card will appear 9. If the corresponding number is 2, the last number of the ID card is the Roman numeral x.
For example: the ID number of a man is [53010219200508011x], let’s see if this ID card is a legal ID card.
First we get the sum of the product of the first 17 bits [(5*7)+(3*9)+(0*10)+(1*5)+(0*8)+(2*4)+(1*2)+(9*1)+(2*6)+(0*3)+(0*7)+(5*9)+(0*10)+(8*5)+(0*8)+(1*4)+(1*2)] is 189, and then divide 189 by 11 is 189/11=17----2, that is, the rest of the numbers are 2. Finally, through the corresponding rules, you can know that the verification code corresponding to remainder 2 is X. Therefore, it can be determined that this is the correct ID number.
The above is excerpted from Baidu Encyclopedia.
This is a related information picture found online.
Based on known information, we can write the internal implementation of this method in js. The first 17-bit verification is easier to implement, so I won’t say much, and focus on the verification code of the last digit.
The code copy is as follows:
// ID number verification
function isIdCard(cardid) {
//Identity card regular expression (18 bits)
var isIdCard2 = /^[1-9]/d{5}(19/d{2}|[2-9]/d{3})((0/d)|(1[0-2]))(([0|1|2]/d)|3[0-1])(/d{4}|/d{3}X)$/i;
var stard = "10X98765432"; //The number of the last ID card
var first = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; //1-17 coefficient
var sum = 0;
if (!isIdCard2.test(cardid)) {
return false;
}
var year = cardid.substr(6, 4);
var month = cardid.substr(10, 2);
var day = cardid.substr(12, 2);
var birthday = cardid.substr(6, 8);
if (birthday != dateToString(new Date(year + '/' + month + '/' + day))) { //Check whether the date is legal
return false;
}
for (var i = 0; i < cardid.length - 1; i++) {
sum += cardid[i] * first[i];
}
var result = sum % 11;
var last = stard[result]; //The last ID number calculated
if (cardid[cardid.length - 1].toUpperCase() == last) {
return true;
} else {
return false;
}
}
//Date to string return date format 2008080808
function dateToString(date) {
if (date instanceof Date) {
var year = date.getFullYear();
var month = date.getMonth() + 1;
month = month < 10 ? '0' + month: month;
var day = date.getDate();
day = day < 10 ? '0' + day: day;
return year + month + day;
}
return '';
}
Only 18-digit ID cards are verified here, and 15-digit first-generation ID cards cannot be used.
Here we also verify the legality of dates, such as illegal dates such as 0230, 0431, and the verification will not be passed.
We can also add this method to jquery validate for easy verification.
Write a custom jquery validate verification method
The code copy is as follows:
// ID number verification
jQuery.validator.addMethod("isIdCard",
function(value, element) {
return this.optional(element) || (isIdCard(value));
},
"Identity card number is illegal!");
Let's have a simple demo to see how it works.
The code copy is as follows:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>
ID number verification
</title>
<script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js">
</script>
<script src="http://www.w3cschool.cc/try/demo_source/static/js/jquery.validate.js">
</script>
<script type="text/javascript">
$(function () {
$("#form1").validate({
rules: {
txtIdCard: "isIdCard"
}
});
});
// ID number verification
function isIdCard(cardid) {
//Identity card regular expression (18 bits)
var isIdCard2 = /^[1-9]/d{5}(19/d{2}|[2-9]/d{3})((0/d)|(1[0-2]))(([0|1|2]/d)|3[0-1])(/d{4}|/d{3}X)$/i;
var stard = "10X98765432"; //The number of the last ID card
var first = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; //1-17 coefficient
var sum = 0;
if (!isIdCard2.test(cardid)) {
return false;
}
var year = cardid.substr(6, 4);
var month = cardid.substr(10, 2);
var day = cardid.substr(12, 2);
var birthday = cardid.substr(6, 8);
if (birthday != dateToString(new Date(year+'/'+month+'/'+day))) {//Check whether the date is legal
return false;
}
for (var i = 0; i < cardid.length - 1; i++) {
sum += cardid[i] * first[i];
}
var result = sum % 11;
var last = stard[result]; //The last ID number calculated
if (cardid[cardid.length - 1].toUpperCase() == last) {
return true;
} else {
return false;
}
}
//Date to string return date format 2008080808
function dateToString(date) {
if (date instanceof Date) {
var year = date.getFullYear();
var month = date.getMonth() + 1;
month = month < 10 ? '0' + month : month;
var day = date.getDate();
day = day < 10 ? '0' + day : day;
return year + month + day;
}
return '';
}
// jquery validate ID number verification
jQuery.validator.addMethod("isIdCard",
function (value, element) {
return this.optional(element) || (isIdCard(value));
},
"Identity card number is illegal!");
</script>
</head>
<body>
<form id="form1" method="get" action="">
<input type="text" id="txtIdCard" name="txtIdCard" />
<p>
<input type="submit" value="submit" />
</p>
</form>
</body>
</html>
Verify with the ID number 53010219200508011X provided on Baidu Encyclopedia
Verification can be passed, try changing x to 0
If the verification fails, the verification method we wrote is successful! If you don’t believe it, try using your ID number. It turns out that I used js to verify the ID number so easy.
The above is all about this article, I hope you like it.