计算校验位

记录一个计算第二代居民身份证最后一位校验位的算法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function calc(str){
var coeff = [7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1];
var suffix = ['1','0','×','9','8','7','6','5','4','3','2'];
var sum = 0;
for(var i = 0;i < 17;i++){
sum += coeff[i] * parseInt(str.charCodeAt(i)-48);
}
sum %= 11;
str = str.substr(0,17) + suffix[sum];
return str;
}
</script>
<button onclick="alert(calc(prompt('请输入待计算的身份证号(前十七位)')))">计算</button>
</body>
</html>

使用

在输入框输入待校验的身份证前17位(身份证共有18位):

image-20220703220416610

点击确定,补全身份证最后一位校验位

image-20220703220609589