ホームページ > 記事 > ウェブフロントエンド > ID番号検証_JavaScriptスキルを実装するjsの簡単な例
以下はIDカード番号のエンコーディングルールに従ったJSを使用した正当性確認コードです
IdCard-Validate.js コードは次のとおりです:
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 card 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; // Will The verification code with the last digit x is replaced 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 by 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 treated directly 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 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 the leading and trailing spaces in the string
function trim(str) {
return str.replace(/(^/s *)|(/s*$)/g, "");
}