1 varassert = Require (' assert ');2Describe (' test ',function() {3It (' COC ',function() {4 varTESTCARD01 = Checkhkidcard ("AB987654 (2)");5Assert (testCard01,true);6 7 varTESTCARD02 = Checkhkidcard ("G123456 (A)");8Assert.equal (testCard02,true);9 Ten varTESTCARD03 = Checkhkidcard ("L555555 (0)"); oneAssert.equal (testCard03,true); a - varTESTCARD04 = Checkhkidcard ("C123456 (9)"); -Assert.equal (testCard04,true); the - varTESTCARD05 = Checkhkidcard ("AY987654 (A)"); -Assert.equal (testCard05,false); - + }); - }) + a at functionCheckhkidcard (str) { - varReg =/^[a-z]{1,2}[0-9]{6}\ ([0-9a]\) $/ - if(!str.match (reg))return false; - - vartemp = str.replace (/[\ (\)]/g, "); - in varLen =temp.length; - varsum = (len = = = 9) to? 9 * (temp[0].charcodeat ()-up) + 8 * (temp[1].charcodeat ()-64) +: 8 * (temp[0].charcodeat ()-64); - the vararr = temp.split ("). reverse (). join ('). substr (1, 6); * varArrresult = [2, 3, 4, 5, 6, 7]; $ Panax Notoginseng for(vari = 0; i< arr.length; i++) { -Sum + = Number (arr[i]) *arrresult[i]; the } + a varMoD = sum% 11; the varLastcode = temp[len-1]; + if(mod = = = 0 && number (lastcode) = = = 0)return true; - $ varCheckcode = 11-mod; $ if(checkcode = = = && Lastcode = = = ' A ')return true; - - if(checkcode > 0 && checkcode < ten && number (lastcode) = = = Checkcode)return true; the return false; -}
JavaScript authentication Hong Kong identity card