The following is the code for validating the ID card number using JS according to the encoding rules
IdCard-Validate.js code is as follows:
/**
* ID card 15-digit encoding rule: dddddd yymmdd xx p
* dddddd: Area code
* yymmdd: Date of birth
* xx: Sequential encoding, cannot be determined
* p : Gender, odd numbers are male, even numbers are female
*
* ID card 18-digit encoding rules: dddddd yyyymmdd xxx y
* dddddd: area code
* yyyymmdd: birth Year, month and day
* xxx: sequential code, cannot be determined, odd numbers are male, even numbers are female
* y: Check code, the value of this digit can be obtained by calculating the first 17 digits
*
* The weighting factor of the 18-digit number is (from right to left) Wi = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2,1 ]
* Verification digit Y = [ 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 ]
* Check digit calculation formula: Y_P = mod( ∑(Ai×Wi),11 )
* i is the 2...18 digits of the ID card number from right to left; Y_P is the position of the check code array where the foot check 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 ]; // 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 digit is correct when the ID number is 18 digits
* @param a_idCard ID card 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 last digit of the verification code with x with 10 to facilitate subsequent operations
}
for ( var i = 0; i < 17; i ) {
sum = Wi[i] * a_idCard[i];// Weighted sum
}
valCodePosition = sum % 11;/ / Get the verification code position
if (a_idCard[17] == ValideCode[valCodePosition]) {
return true;
} else {
return false;
}
}
/**
* Determine whether you are a boy or a girl based on your 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. 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;
}
// The incoming characters can be directly treated as an array To handle
// 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 card number is a valid birthday
* @param idCard 18-digit ID card 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 Y2K problem
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 card number is a valid birthday
* @param idCard15 15-digit ID card 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 the old ID card, there is no need to consider the Y2K issue 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 spaces at the beginning and end of the string
function trim(str) {
return str.replace(/(^s* )|(s*$)/g, "");
}