The javascript implementation reads the relevant information based on the id number
- 2020-05-07 19:12:14
- OfStack
The citizenship number consists of a 6-digit address code, an 8-digit birth date code, a 3-digit sequence code and a 1-digit checksum code.
The first two are provinces, the middle two are cities, and the last two are counties
The sequence code refers to the sequence number of persons born in the same year, month and day within the area identified by the 1 address code. Odd Numbers of sequence codes are given to men and even Numbers to women.
The check code is the check code calculated according to the previous 107 digit code and ISO 7064:1983.MOD 11-2 check code.
Check code calculation method:
1) multiply the previous 17 digits of the id number by different coefficients, and the coefficients from the first digit to the 107th digit are: 7 9, 10 5, 8, 4, 2, 6, 3, 7 9, 10, 5, 8, 4, 2;
2) add these 17 digits to the result of multiplying the coefficients. Divide the sum by 11 to get the remainder.
4) the remainder can only have the 11 digits of 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, which correspond to the number of the last 1 id card of 1, 0 X 9, 8, 7, 6, 5, 4, 3, 2.
Validation of id Numbers supports 15-bit and 18-bit id Numbers to support address coding, date of birth, and checksum bit authentication
<div style="padding:20px 40px;">
<h1 style="font-size:20px;color:#999;"> Id card enquiry </h1>
<input type="text" placeholder=" Enter your id number " id="code">
<input type="button" value=" The query " id="btn">
<p id="home"><strong> cadastral Penetration: </strong><span></span></p>
<p id="birthday"><strong> Date of birth: </strong><span></span></p>
<p id="sex"><strong> sex Don't: </strong><span></span></p>
</div>
<script type="text/javascript">
// Remove Spaces from the beginning and end of a string
var home='',birthday='',sex='';
function trim(str) {
return str.replace(/^\s*|\s*$/g, "");
}
// Verification of identity card
function IdentityCodeValid(code) {
code=trim(code);
var city={11:" Beijing ",12:" tianjin ",13:" hebei ",14:" shanxi ",15:" Inner Mongolia ",21:" liaoning ",22:" Ji Lin ",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:"4 sichuan ",52:" guizhou ",53:" yunnan ",54:" Tibet ",61:" shaanxi ",62:" gansu ",63:" qinghai ",64:" ningxia ",65:" xinjiang ",71:" Taiwan ",81:" Hong Kong ",82:" Macau "};
if(!code || !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(code)){
alert(" Wrong id number format ");
home='',birthday='',sex='';
return false;
}
if(!city[code.substring(0,2)]){
alert(" Address coding error ");
home='',birthday='',sex='';
return false;
}
if(code.length == 18){ //18 Bit id card needs to be verified finally 1 A check digit
var codeArr = code.split('');
var factor = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ]; // The weighting factor
var parity = [ 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 ]; // Check digit
var sum = 0;
for (var i = 0; i < 17; i++){
sum += codeArr[i] * factor[i];
}
if(parity[sum % 11] != codeArr[17]){
alert(" Check bit error ");
home='',birthday='',sex='';
return false;
}
}
// provinces
home = city[code.substring(0,2)];
// birthday
birthday = code.substring(6,10)+' years '+code.substring(10,12)+' month '+code.substring(12,14)+' day ';
// gender
if(code.length==15){
sex = code.substring(14,15)%2==0 ? ' female ':' male ';
}else if(code.length==18){
sex = code.substring(14,17)%2==0 ? ' female ':' male ';
}
}
// The output
document.querySelector('#btn').onclick=function(){
var code=document.querySelector('#code').value;
IdentityCodeValid(code);
document.querySelector('#home span').innerHTML=home;
document.querySelector('#birthday span').innerHTML=birthday;
document.querySelector('#sex span').innerHTML=sex;
}
</script>
This paper is mainly based on the rules of the citizen id card, read the relevant identity information, very convenient and practical, recommended to you.