function getIdCardInfo(idcard){ var sigma = 0; var a = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ); var w = new Array("1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"); for(var i = 0; i < 17; i++) { var ai = parseInt(idcard.substring(i, i + 1)); var wi = a[i]; sigma += ai * wi; } var number = sigma % 11; return idcard.substring(0,17)+w[number]; } function idCard15To18(id){ var W = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1); var A = new Array("1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"); var i,j,s=0; var newid; newid = id; newid = newid.substring(0,6)+"19"+newid.substring(6,id.length); for(i=0;i<newid.length;i++ ){ j= parseInt(newid.substring(i,i+1))*W[i]; s=s+j; } s = s % 11; newid=newid+A[s]; return newid; }
js验证,value代码身份证号码
if(value.length==18){ return getIdCardInfo(value)==value; }else if(value.length==15){ var id18=idCard15To18(value); return getIdCardInfo(id18)==value; }else{ return false; }