Home > Web Front-end > JS Tutorial > Specific implementation method of ultra-accurate JavaScript verification of ID number_javascript skills

Specific implementation method of ultra-accurate JavaScript verification of ID number_javascript skills

WBOY
Release: 2016-05-16 15:31:42
Original
1432 people have browsed it

Before writing the program, let’s first understand the composition of the ID number:

There are two types of ID numbers, the old one is 15 digits and the new one is 18 digits.

ID card 15-digit encoding rule: dddddd yymmdd xx p

Among them dddddd: area code yymmdd: date of birth xx: sequential code, cannot be determined p: gender, odd numbers are male, even numbers are female;

ID card 18-bit encoding rule: dddddd yyyymmdd xxx y

Among them dddddd: area code yyyymmdd: year, month and day of birth 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 calculation formula See the program, Some constants that need to be used :

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 bit 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 in 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 ]; // 加权因子 
var ValideCode = [ 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 ];   // 身份证验证位值.10代表X 
function IdCardValidate(idCard) { 
 idCard = trim(idCard.replace(/ /g, ""));    //去掉字符串头尾空格      
 if (idCard.length == 15) { 
  return isValidityBrithBy15IdCard(idCard);  //进行15位身份证的验证 
 } else if (idCard.length == 18) { 
  var a_idCard = idCard.split("");    // 得到身份证数组 
  if(isValidityBrithBy18IdCard(idCard)&&isTrueValidateCodeBy18IdCard(a_idCard)){ //进行18位身份证的基本验证和第18位的验证
   return true; 
  }else { 
   return false; 
  } 
 } else { 
  return false; 
 } 
} 
/** 
 * 判断身份证号码为18位时最后的验证位是否正确 
 * @param a_idCard 身份证号码数组 
 * @return 
 */ 
function isTrueValidateCodeBy18IdCard(a_idCard) { 
 var sum = 0;        // 声明加权求和变量 
 if (a_idCard[17].toLowerCase() == 'x') { 
  a_idCard[17] = 10;     // 将最后位为x的验证码替换为10方便后续操作 
 } 
 for ( var i = 0; i < 17; i++) { 
  sum += Wi[i] * a_idCard[i];   // 加权求和 
 } 
 valCodePosition = sum % 11;    // 得到验证码所位置 
 if (a_idCard[17] == ValideCode[valCodePosition]) { 
  return true; 
 } else { 
  return false; 
 } 
} 
/** 
 * 验证18位数身份证号码中的生日是否是有效生日 
 * @param idCard 18位书身份证字符串 
 * @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)); 
 // 这里用getFullYear()获取年份,避免千年虫问题 
 if(temp_date.getFullYear()!=parseFloat(year) 
   ||temp_date.getMonth()!=parseFloat(month)-1 
   ||temp_date.getDate()!=parseFloat(day)){ 
   return false; 
 }else{ 
  return true; 
 } 
} 
 /** 
 * 验证15位数身份证号码中的生日是否是有效生日 
 * @param idCard15 15位书身份证字符串 
 * @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)); 
  // 对于老身份证中的你年龄则不需考虑千年虫问题而使用getYear()方法 
  if(temp_date.getYear()!=parseFloat(year) 
    ||temp_date.getMonth()!=parseFloat(month)-1 
    ||temp_date.getDate()!=parseFloat(day)){ 
    return false; 
  }else{ 
   return true; 
  } 
 } 
//去掉字符串头尾空格 
function trim(str) { 
 return str.replace(/(^\s*)|(\s*$)/g, ""); 
} 
Copy after login

Determine gender based on ID number

/** 
 * 通过身份证判断是男是女 
 * @param idCard 15/18位身份证号码 
 * @return 'female'-女、'male'-男 
 */ 
function maleOrFemalByIdCard(idCard){ 
 idCard = trim(idCard.replace(/ /g, ""));  // 对身份证号码做处理。包括字符间有空格。 
 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; 
 } 
} 
Copy after login

The above is the specific implementation method of javascript to verify the ID number, and the gender can be verified based on the ID card. Isn’t it amazing? Everyone is welcome to learn from it.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template