JavaScriptは18桁または15桁の身分証番号が合法的かどうかを検証します.


お客様はフロントで身分証の検証を強化するように要求していますが、残念ながらこれまで検討したことがありません.せいぜい簡単な規則で検証を完成します.この検証を完成させるために、身分証明書(18桁)のアルゴリズムを理解しました.修繕は機能を補完しました.また少し勉強します.私はただの初心者です.コードを贴り付けますので、皆さんの指点を期待しています.あなたのコメントの中で少し進歩したいです.ありがとうございます.
 

  
  
  
  
  1. /*   
  2.      
  3.      *QQ:909507090 
  4.      *var ck=new Qhjsw.IDChecks(" "); 
  5.      *var tip=ck.IDCk(); 
  6.      *tip (object) 7  
  7.      *msg  , 。 : 、  
  8.      *pass  bool ,  。 true ,false  
  9.      *idcode  ,  
  10.      *address  ( ),  
  11.      *sextag   , 。0 ,1  
  12.      *sex      。  
  13.      *birthday   18 YYYY-MM-DD 15 YY-MM-DD  :1991-01-01(18 )、91-01-01(15 ) 
  14.      * 
  15.      * 
  16.      * , , ,     
  17.      * , 17    
  18.      * 18 ( ) Wi = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2,1 ]   
  19.      *   Y = [ 1, 0,'X' , 9, 8, 7, 6, 5, 4, 3, 2 ]    
  20.      *  :Y_P = mod( ∑(Ai×Wi),11 )    
  21.     */ 
  22.        var Qhjsw = {}; 
  23.        Qhjsw.IDChecks = function (Code) { 
  24.            if(typeof Code!="string"
  25.            { 
  26.                 throw new Error(" !"); 
  27.            } 
  28.            var city = { 11: " ", 12: " ", 13: " ", 14: " ", 15: " ", 21: " ", 22: " ", 23: "  ", 31: " ", 32: " ", 33: " ", 34: " ", 35: " ", 36: " ", 37: " ", 41: " ", 42: "  ", 43: " ", 44: " ", 45: " ", 46: " ", 50: " ", 51: " ", 52: " ", 53: " ", 54: "  ", 61: " ", 62: " ", 63: " ", 64: " ", 65: " ", 71: " ", 81: " ", 82: " ", 91: "  " }; 
  29.            var Tip = {};   // ,  
  30.            var IDCode = Code; 
  31.            var code = [];  // ,  
  32.            var sextable=[" "," "];   // , 。  
  33.            var Ck = function (IDCode) { 
  34.                if (!IDCode ||!/(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^\d{6}(18|19|20){1}\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X|x)$)/i.test(IDCode)) { 
  35.                    Tip.msg = " "
  36.                    Tip.pass = false
  37.                } 
  38.                else if (!city[IDCode.substr(0, 2)]) { 
  39.                    Tip.msg = " "
  40.                    Tip.pass = false
  41.                } 
  42.                else if(!_CheckBirthday(IDCode)) 
  43.                { 
  44.                    Tip.msg=" "
  45.                    Tip.pass=false
  46.                } 
  47.                else { 
  48.  
  49.                    if (18 == IDCode.length) { 
  50.                        code = IDCode.split(''); 
  51.                        var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; 
  52.                        var parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2]; 
  53.                        var sum = 0; 
  54.                        var ai = 0; 
  55.                        var wi = 0; 
  56.                        if(typeof code[17]=="string"
  57.                        { 
  58.                            code[17]=code[17].toUpperCase(); 
  59.                        } 
  60.                        for (var i = 0; i < 17; i++) { 
  61.                            ai = code[i]; 
  62.                            wi = factor[i]; 
  63.                            sum += ai * wi; 
  64.                        } 
  65.                        var last = parity[sum % 11]; 
  66.                        if (parity[sum % 11] != code[17]) { 
  67.                            Tip.msg = " "
  68.                            Tip.pass = false
  69.                        } 
  70.                        else { 
  71.                            Tip.msg = " "
  72.                            Tip.pass = true
  73.                            Tip.idcode = IDCode; 
  74.                            Tip.address = city[IDCode.substr(0, 2)]; 
  75.                            Tip.sextag=_GetSex(IDCode); 
  76.                            Tip.sex=sextable[Tip.sextag]; 
  77.                            Tip.birthday=_GetBirthday(IDCode); 
  78.                             
  79.                        } 
  80.                    } 
  81.                    else { 
  82.                        Tip.msg = " "
  83.                        Tip.pass = true
  84.                        Tip.idcode = IDCode; 
  85.                        Tip.address = city[IDCode.substr(0, 2)]; 
  86.                        Tip.sextag=_GetSex(IDCode); 
  87.                        Tip.sex=sextable[Tip.sextag]; 
  88.                        Tip.birthday=_GetBirthday(IDCode); 
  89.                    } 
  90.                } 
  91.            }; 
  92.            this.IDCk = function () { 
  93.                Ck(IDCode); 
  94.                return  Tip; 
  95.            }; 
  96.           function _CheckBirthday(IDCodes) 
  97.           { 
  98.               if(IDCodes.length==18) 
  99.               { 
  100.                  var year =  IDCodes.substring(6,10);    
  101.                  var month = IDCodes.substring(10,12);    
  102.                  var day = IDCodes.substring(12,14);    
  103.                  var temp_date = new Date(year,parseFloat(month)-1,parseFloat(day));    
  104.                  //  getFullYear() ,     
  105.                 if(temp_date.getFullYear()!=parseFloat(year)||temp_date.getMonth()!=parseFloat(month)-1    
  106.                      ||temp_date.getDate()!=parseFloat(day)){    
  107.                         return  false;    
  108.                  } 
  109.                  else{    
  110.                         return true;    
  111.                 }    
  112.               } 
  113.               else
  114.                     var year =  IDCodes.substring(6,8);    
  115.                     var month = IDCodes.substring(8,10);    
  116.                     var day = IDCodes.substring(10,12);    
  117.                     var temp_date = new Date(year,parseFloat(month)-1,parseFloat(day));    
  118.                     //  getYear()     
  119.                    if(temp_date.getYear()!=parseFloat(year)||temp_date.getMonth()!=parseFloat(month)-1    
  120.                      ||temp_date.getDate()!=parseFloat(day)){    
  121.                         return false;    
  122.                     } 
  123.                     else{    
  124.                          return true;    
  125.                      }    
  126.               } 
  127.           } 
  128.           function _GetBirthday(IDCodes){ 
  129.              if(IDCodes.length==18){ 
  130.                  var year =  IDCodes.substring(6,10);    
  131.                  var month = IDCodes.substring(10,12);    
  132.                  var day = IDCodes.substring(12,14); 
  133.                  return year+"-"+month+"-"+day;   
  134.              } 
  135.              else 
  136.              { 
  137.                 var year =  IDCodes.substring(6,8);    
  138.                 var month = IDCodes.substring(8,10);    
  139.                 var day = IDCodes.substring(10,12); 
  140.                 return year+"-"+month+"-"+day; 
  141.                  
  142.              } 
  143.           } 
  144.           function _GetSex(IDCodes){ 
  145.               if(IDCodes.length==18){ 
  146.                 if(IDCodes.substring(14,17)%2==0){    
  147.                      return 0;    
  148.                 } 
  149.                 else{    
  150.                      return 1; 
  151.                 }    
  152.              } 
  153.              else 
  154.              { 
  155.                  if(IDCodes.substring(14,15)%2==0){    
  156.                     return 0;    
  157.                  } 
  158.                  else{    
  159.                    return  1;    
  160.                  }    
  161.              } 
  162.           } 
  163.  
  164.        }; 
最近はJavaScriptを強化したいですが、いい本がたくさんありますか?ありがとうございます.
 
この記事の転載:http://www.cnblogs.com/bluescreen/archive/2012/06/16/2551959.html