The following is the validity verification code using JS according to the ID number encoding rules.
The IdCard-Validate.js code is as follows:
The code copy is as follows:
/**
* 15-bit encoding rules for ID card: dddddd yymmdd xx p
* dddddd: Regional code
* yymmdd: date of birth
* xx: Sequential class encoding, cannot be determined
* p: Gender, odd numbers are male, even numbers are female
* <p />
* 18-bit encoding rules for ID card: dddddd yyyymmdd xxx y
* dddddd: Regional code
* yyyymmdd: date of birth
* xxx: Sequence type encoding, cannot be determined, odd numbers are males, even numbers are females
* y: Check code, the value of this digit can be obtained through the first 17 digits
* <p />
* The weighting factor of 18-digit number is (right to left) Wi = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 ]
* Verification bit Y = [ 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 ]
* Calculation formula for check bit: Y_P = mod( ∑(Ai×Wi),11 )
* i is the 2...18 digits of the ID number from right to left; Y_P is the verification code array position where the foot verification code is located
*
*/
var Wi = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 ];// Weighting factor
var ValideCode = [ 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 ];// The ID card 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 the ID card array
if(isValidityBrithBy18IdCard(idCard)&&isTrueValidateCodeBy18IdCard(a_idCard)){
return true;
}else {
return false;
}
} else {
return false;
}
}
/**
* Determine whether the last verification bit is correct when the ID number is 18 digits
* @param a_idCard ID number array
* @return
*/
function isTrueValidateCodeBy18IdCard(a_idCard) {
var sum = 0; // Declare weighted sum variable
if (a_idCard[17].toLowerCase() == 'x') {
a_idCard[17] = 10;// Replace the verification code with the last bit x with 10 for subsequent operations
}
for ( var i = 0; i < 17; i++) {
sum += Wi[i] * a_idCard[i];// Weighted sum
}
valCodePosition = sum % 11;// Where to get the verification code
if (a_idCard[17] == ValideCode[valCodePosition]) {
return true;
} else {
return false;
}
}
/**
*Judge whether it is a man or a woman through ID card
* @param idCard 15/18-digit ID number
* @return 'female'-female, 'male'-male
*/
function maleOrFemalByIdCard(idCard){
idCard = trim(idCard.replace(/ /g, ""));// Process the ID number. Including 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 processed directly as an array
// if(idCard.length==15){
// alert(idCard[13]);
// if(idCard[13]%2==0){
// return 'female';
// }else{
// return 'male';
// }
// }else if(idCard.length==18){
// alert(idCard[16]);
// if(idCard[16]%2==0){
// return 'female';
// }else{
// return 'male';
// }
// }else{
// return null;
// }
}
/**
* Verify whether the birthday in the 18-digit ID number is a valid birthday
* @param idCard 18-bit book ID string
* @return
*/
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));
// Use getFullYear() here to get the year to avoid the problem of millennium bugs
if(temp_date.getFullYear()!=parseFloat(year)
||temp_date.getMonth()!=parseFloat(month)-1
||temp_date.getDate()!=parseFloat(day)){
return false;
}else{
return true;
}
}
/**
* Verify whether the birthday in the 15-digit ID number is a valid birthday
* @param idCard15 15-bit book ID string
* @return
*/
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 in your old ID card, you don’t need to consider the problem of Millennium Worm and use the getYear() method
if(temp_date.getYear()!=parseFloat(year)
||temp_date.getMonth()!=parseFloat(month)-1
||temp_date.getDate()!=parseFloat(day)){
return false;
}else{
return true;
}
}
//Remove the string head and tail spaces
function trim(str) {
return str.replace(/(^/s*)|(/s*$)/g, "");
}
Updated version 2
The code copy is as follows:
function checkIdcard(num)
{
num = num.toUpperCase();
//The ID card number is 15 or 18 digits. When 15 digits, it is all numbers. The first 17 digits of 18 digits are numbers. The last digit is a check digit, which may be a number or character X.
if (!(/(^/d{15}$)|(^/d{17}([0-9]|X)$)/.test(num))))
{
//alert('The length of the entered ID number is incorrect, or the number does not meet the requirements! /n15-digit numbers should all be numbers, and the last 18-digit number can be numbers or X.');
return false;
}
//The check bit is generated according to the provisions of ISO 7064:1983.MOD 11-2, and X can be considered as the number 10.
//The following analyzes the date of birth and check digits respectively
var len, re;
len = num.length;
if (len == 15)
{
re = new RegExp(/^(/d{6})(/d{2})(/d{2})(/d{2})(/d{2})(/d{3})$/);
var arrSplit = num.match(re);
//Check whether the birthday date 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() == N umber( arrSplit[4]));
if (!bGoodDay)
{
//alert('The date of birth in the entered ID number is incorrect!');
return false;
}
else
{
//Convert 15-digit ID card to 18-digit
//The check bit is generated according to the provisions of 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 whether the birthday date 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(dtmBirth.getYear());
//alert(arrSplit[2]);
//alert('The date of birth in the entered ID number is incorrect!');
return false;
}
else
{
//Check whether the verification code of the 18-digit ID card is correct.
//The check bit is generated according to the provisions of 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('The verification code of the 18-bit ID card is incorrect! It should be: ' + valnum);
return false;
}
return true;
}
}
return false;
}