<!DOCTYPE html> <html> <head> <title>香港身份证校验码计算</title> </head> <body> <input type=text id="no" value="A1234569" /> <div>正确证号为:<label id="result"></label></div> <button onclick="generate()">计算</button> <script> function generate(){ var str = document.getElementById("no").value; if(str.length!=8){alert("输入不正确!");return;} var vNo = generateValidCode(str); document.getElementById("result").innerHTML = str.substring(0,str.length - 1) + vNo; } function generateValidCode(str){ var hash = (str.substring(0,1).toUpperCase().charCodeAt() - 64) * 8; for(var i = 1;i<=6;i++) { hash += parseInt(str.substring(i,i+1)) * (8-i) } var validateNo = hash%11; var ret = ""; if(validateNo==1){ ret = "A"; }else { ret = 11 - validateNo; } return ret; } </script> </body> </html>