# JavaScript to verify the correctness of the last bit of the 18 bit id number

• 2020-03-30 03:38:59
• OfStack

Calculate the correctness of the last digit according to the id number, if not correct will also give the correct result, very interesting a break procedure.

``````
<!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">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>javascript 18 Bit id number last check code </title>

<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(' You enter the 18 The id number is correct! <br> Birthday: ' + id.substr(6, 8) + '<br> Gender: ' + [' female ', ' male '][parseInt(id.charAt(16)) % 2]);
else showRst(' You enter the 18 Id card number check code error, 18 Bit check code should be ' + rc + ' ! ');
}
else showRst(' Please enter the 18 Digit id number! ');
}
function showRst(msg) {document.getElementById('rst').innerHTML=msg }
</script>
<input type="text" onblur="ValidID(this.value)" />
<div id="rst"></div>

</body>
</html>
``````

Related articles: