Parsing ID number

// Analyzing ID number and age of the format 
Export function authenticationAccount (IDNUM) {
   const City = {
     . 11 : ' Beijing ' ,
     12 : ' Tianjin ' ,
     13 : ' Hebei ' ,
     14 : ' Shanxi ' ,
     15 : ' Inner Mongolia ' ,
     21 : ' Liaoning ' ,
     22 : ' Jilin ' ,
     23 : 'Heilongjiang ' ,
     31 : " Shanghai " ,
     32 : " Jiangsu ' ,
     33 : ' Zhejiang ' ,
     34 : ' Anhui ' ,
     35 : ' Fujian ' ,
     36 : ' Jiangxi ' ,
     37 : ' Shandong ' ,
     41 : ' Henan ' ,
     42 : 'Hubei ',
     43 : ' Hunan ' ,
     44 : ' Guangdong ' ,
     45 : ' Guangxi ' ,
     46 : ' Hainan ' ,
     50 : ' Chongqing ' ,
     51 : ' Sichuan ' ,
     52 : ' Guizhou ' ,
     53 : ' Yunnan ' ,
     54 : ' Tibet ' ,
    61 : ' Shaanxi ' ,
     62 : ' Gansu ' ,
     63 : ' Qinghai ' ,
     64 : ' Ningxia ' ,
     65 : ' Xinjiang ' ,
     71 : ' Taiwan ' ,
     81 : ' Hong Kong ' ,
     82 : ' Macau ' ,
     91 : ' foreign ' , 
  };
  Tip the let ;= ' Verification is successful " ; 
  the let isRight = to true ; 
  the let Sex = ' ' ; 
  the let Age = 0 ; 
  the let code = [];
   IF (idNum.length ==! 18 is ) { 
    Tip = ' ID number format error ' ; 
    isRight = to false ; 
  } the else  IF (City [idNum.substr (! 0 , 2 )]) { 
    Tip = ' address code error ' 
    isRight = to false ; 
  } the else {
     // 18 is required to verify the last one-bit ID parity 
    code = idNum.split ( '' );
     // [Sigma (the Wi × AI) (MOD. 11)
     // weighting factor 
    const factor = [ 7 , 9 , 10 , 5 , 8 , 4 , 2 , 1 , 6 , 3 , 7 , 9 , 10 , 5 , 8 , 4 , 2 ];
     // 校验位
    const parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
    let sum = 0;
    let ai = 0;
    let wi = 0;
    for (let i = 0; i < 17; i++) {
      ai = code[i];
      wi = ) { factor[i];
      SUM + AI * = Wi; 
    } 
    // var Last Parity = [SUM%. 11]; 
    IF (Parity [SUM% . 11 !] .toString () == code [ . 17 ] .toString ()) { 
      Tip = ' check bit error ' ; 
      isRight = to false ; 
    } 

    IF (isRight) {
       // ID number parsing 
      const codeStr = idNum.toString ();
       IF (the parseInt (codeStr.substr ( 16 , . 1 ), 10 )% 2 === . 1 
        Sex ='';
      } else {
        sex = '';
      }
      // 获取年龄
      const myDate = new Date();
      const month = myDate.getMonth() + 1;
      const day = myDate.getDate();
      age = myDate.getFullYear() - codeStr.substring(6, 10) - 1;
      const codeMonth = parseInt(codeStr.substring(10, 12), 10);
      const codeDay = parseInt(codeStr.substring(12, 14), 10);
      if (codeMonth < month || (codeMonth === month && codeDay <= day)) {
        age++;
      }
    }
  }
  return {
    isRight,
    age,
    sex,
    tip,
  };
}

 

Guess you like

Origin www.cnblogs.com/wangyuxue/p/11468410.html