# Js verify the validity of id information

• 2020-03-30 02:29:35
• OfStack

The following is the code code for the validity verification of the id number according to the code rules

Idcard-validate. Js code is as follows:

/**
*  Id card 15 Bit coding rules: dddddd yymmdd xx p
* dddddd : the area code
* yymmdd:  Date of birth
* xx:  The sequence class is encoded, cannot be determined
* p:  Gender, odd number is male, even number is female
* <p />
*  Id card 18 Bit coding rules: dddddd yyyymmdd xxx y
* dddddd : the area code
* yyyymmdd:  Date of birth
* xxx: The order class code, can't be determined, the odd number is male, the even number is female
* y:  Check code, the bit value can pass before 17 Bit calculation
* <p />
* 18 The bit number weighting factor is ( From right to left ) Wi = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2,1 ]
*  To verify a  Y = [ 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 ]
*  Check bit calculation formula: Y_P = mod(  ∑ (Ai x Wi),11 )
* i Number for id card from right to left  2...18  position ; Y_P Is the position of the check code array of the foot check code
*
*/

var Wi = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 ];//The weighting factor
var ValideCode = [ 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 ];//Id verification bit value.10 represents X
function IdCardValidate(idCard) {
idCard = trim(idCard.replace(/ /g, ""));
if (idCard.length == 15) {
return isValidityBrithBy15IdCard(idCard);
} else if (idCard.length == 18) {
var a_idCard = idCard.split("");//Get id array
if(isValidityBrithBy18IdCard(idCard)&&isTrueValidateCodeBy18IdCard(a_idCard)){
return true;
}else {
return false;
}
} else {
return false;
}
}

function isTrueValidateCodeBy18IdCard(a_idCard) {
var sum = 0; //Declare a weighted sum variable
if (a_idCard[17].toLowerCase() == 'x') {
a_idCard[17] = 10;//Replace the last bit of verification code with x with 10 for subsequent operations
}
for ( var i = 0; i < 17; i++) {
sum += Wi[i] * a_idCard[i];//The weighted sum
}
valCodePosition = sum % 11;//Get the captcha location
if (a_idCard[17] == ValideCode[valCodePosition]) {
return true;
} else {
return false;
}
}
/**
*  It's a man or a woman by the id card
* @param idCard 15/18 Id card number
* @return 'female'- Female, 'male'- male
*/
function maleOrFemalByIdCard(idCard){
idCard = trim(idCard.replace(/ /g, ""));//Handle the id number. Include Spaces between characters.
if(idCard.length==15){
if(idCard.substring(14,15)%2==0){
return 'female';
}else{
return 'male';
}
}else if(idCard.length ==18){
if(idCard.substring(14,17)%2==0){
return 'female';
}else{
return 'male';
}
}else{
return null;
}
//Incoming characters can be treated directly as arrays
// if(idCard.length==15){
// if(idCard[13]%2==0){
// return 'female';
// }else{
// return 'male';
// }
// }else if(idCard.length==18){
// if(idCard[16]%2==0){
// return 'female';
// }else{
// return 'male';
// }
// }else{
// return null;
// }
}

function isValidityBrithBy18IdCard(idCard18){
var year = idCard18.substring(6,10);
var month = idCard18.substring(10,12);
var day = idCard18.substring(12,14);
var temp_date = new Date(year,parseFloat(month)-1,parseFloat(day));
//GetFullYear () is used to get the year to avoid the y2k problem
if(temp_date.getFullYear()!=parseFloat(year)
||temp_date.getMonth()!=parseFloat(month)-1
||temp_date.getDate()!=parseFloat(day)){
return false;
}else{
return true;
}
}

function isValidityBrithBy15IdCard(idCard15){
var year = idCard15.substring(6,8);
var month = idCard15.substring(8,10);
var day = idCard15.substring(10,12);
var temp_date = new Date(year,parseFloat(month)-1,parseFloat(day));
//For your age on the old id card, use the getYear() method instead of the y2k problem
if(temp_date.getYear()!=parseFloat(year)
||temp_date.getMonth()!=parseFloat(month)-1
||temp_date.getDate()!=parseFloat(day)){
return false;
}else{
return true;
}
}
//Remove Spaces from the beginning and end of a string
function trim(str) {
return str.replace(/(^s*)|(s*\$)/g, "");
}

Updated version 2

function checkIdcard(num)
{
num = num.toUpperCase();
//The id number is 15 or 18 digits, 15 digits are all digits, 18 digits the first 17 digits are digits, the last digit is a checksum digit, may be a digit or character X.
if (!(/(^d{15}\$)|(^d{17}([0-9]|X)\$)/.test(num)))
{
//Alert (' entered the wrong length of the identification number, or the number does not meet the regulations! n15 digits should be all digits, 18 digits can be digits or X. ');
return false;
}
//The check bit is generated according to ISO 7064:1983.mod 11-2, and X can be considered as the number 10.
//The date of birth and check bits are analyzed below
var len, re;
len = num.length;
if (len == 15)
{
re = new RegExp(/^(d{6})(d{2})(d{2})(d{2})(d{3})\$/);
var arrSplit = num.match(re);

//Check that the date of birth is correct
var dtmBirth = new Date('19' + arrSplit[2] + '/' + arrSplit[3] + '/' + arrSplit[4]);
var bGoodDay;
bGoodDay = (dtmBirth.getYear() == Number(arrSplit[2])) && ((dtmBirth.getMonth() + 1) == Number(arrSplit[3])) && (dtmBirth.getDate() == Number(arrSplit[4]));
if (!bGoodDay)
{
//Alert (' wrong date of birth in id number! ');
return false;
}
else
{
//Convert 15 id CARDS into 18 id CARDS
//The check bit is generated according to ISO 7064:1983.mod 11-2, and X can be considered as the number 10.
var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
var nTemp = 0, i;
num = num.substr(0, 6) + '19' + num.substr(6, num.length - 6);
for(i = 0; i < 17; i ++)
{
nTemp += num.substr(i, 1) * arrInt[i];
}
num += arrCh[nTemp % 11];
return true;
}
}
if (len == 18)
{
re = new RegExp(/^(d{6})(d{4})(d{2})(d{2})(d{3})([0-9]|X)\$/);
var arrSplit = num.match(re);

//Check that the date of birth is correct
var dtmBirth = new Date(arrSplit[2] + "/" + arrSplit[3] + "/" + arrSplit[4]);
var bGoodDay;
bGoodDay = (dtmBirth.getFullYear() == Number(arrSplit[2])) && ((dtmBirth.getMonth() + 1) == Number(arrSplit[3])) && (dtmBirth.getDate() == Number(arrSplit[4]));
if (!bGoodDay)
{
//Alert (' wrong date of birth in id number! ');
return false;
}
else
{
//Check whether the check code of 18 id card is correct.
//The check bit is generated according to ISO 7064:1983.mod 11-2, and X can be considered as the number 10.
var valnum;
var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
var nTemp = 0, i;
for(i = 0; i < 17; i ++)
{
nTemp += num.substr(i, 1) * arrInt[i];
}
valnum = arrCh[nTemp % 11];
if (valnum != num.substr(17, 1))
{
//Alert (' 18-digit id card check code is incorrect! Should be: '+ valnum);
return false;
}
return true;
}
}
return false;
}

Related articles: