根据身份证号码推算出最后一位的正确性,如果不正确还会给出正确结果,非常有意思的一断程序。
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
26
27
28
29
30
|
<!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>
|