1. 程式人生 > >計算身份證校驗碼(最後一位)

計算身份證校驗碼(最後一位)

map git 校驗碼 meta ffi spa class ++ msg

在線預覽

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    #msg{
      color: red;
    }
  </style>
</head>
<body>
  身份證號:<input id="id-card" type="text">(前17位)<br>
  (校驗位)最後一位為:<span
id="msg"></span><span id="check-digit"></span><br> <button onclick="calculate()">計算</button><br> <script> "use strict"; // 前17位每項的系數 let coefficient = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; // 除11取余的結果對應的校驗位(最後一位)的值 let
checkDigitMap = [‘1‘, ‘0‘, ‘X‘, ‘9‘, ‘8‘, ‘7‘, ‘6‘, ‘5‘, ‘4‘, ‘3‘, ‘2‘]; function calculate(){ var idCard = document.querySelector("#id-card"); var checkDigit = document.querySelector("#check-digit"); var msg = document.querySelector("#msg"); var sum = 0; checkDigit.innerHTML
= msg.innerHTML = ""; if(idCard.value.length < 17){ msg.innerHTML = "至少輸入身份證前17位"; return } for (var i = 0; i < 17; i++) { sum += idCard.value[i] * coefficient[i]; } checkDigit.innerHTML = checkDigitMap[sum % 11]; } </script> </body> </html>

計算身份證校驗碼(最後一位)