Home > Web Front-end > JS Tutorial > JavaScript implementation code to verify the correctness of the last digit of the 18-digit ID number_javascript skills

JavaScript implementation code to verify the correctness of the last digit of the 18-digit ID number_javascript skills

WBOY
Release: 2016-05-16 16:40:13
Original
1372 people have browsed it

Calculate the accuracy of the last digit based on the ID number. If it is incorrect, the correct result will be given. It is a very interesting program.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>javascript 18位身份证号码最后一位校验码</title>
</head>

<body>

<script>
  function getIDChar18(id) {
    var arr = id.split(''), sum = 0, vc = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
    for (var i = 0; i < 17; i++) sum += vc[i] * parseInt(arr[i]);
    return ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'][sum % 11];
  }
  function ValidID(id) {
    if (/^\d{18}$/.test(id)) {
      var c = id.charAt(17), rc = getIDChar18(id);
      if (c == rc) showRst('您输入的18位身份证号码正确!<br>生日:' + id.substr(6, 8) + '<br>性别:' + ['女', '男'][parseInt(id.charAt(16)) % 2]);
      else showRst('您输入的18位身份证号码检验码错误,18位校验码应该为' + rc + '!');
    }
    else showRst('请输入18位数字的身份证号码!');
  }
  function showRst(msg) {document.getElementById('rst').innerHTML=msg }
</script>
<input type="text" onblur="ValidID(this.value)" />
<div id="rst"></div>

</body>
</html>
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template