The following is the validity verification code using JS according to the ID number encoding rules.
The IdCard-Validate.js code is as follows:
Copy code code 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
*/
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;
}
//The 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, "");
}
For the above code, in actual use of gender, you can first determine whether the ID card is valid. This kind of judgment is not made in this code example, and it feels a bit useless and can be fully enriched according to the situation in actual use.