## 身份证最后一位算法 JavaScript实现

1. 将下面代码复制到记事本中，保存文件名为 *.html
2. 使用浏览器打开就可以使用了
``````<html>
<title>JavaScript</title>
<body>
请输入身份证号码：<br>
<input type="text" name="card"><br>
<input type="button" value="验证18位身份证" οnclick="submit1();">
<input type="button" value="17位转18位身份证" οnclick="submit2();">
<input type="button" value="预留按钮" οnclick="submit3();">
<br><br>身份证号验证脚本 作者：xzlAwin<br>
<script type="text/javascript">
function submit3()
{
}
function submit1()
{
var card = document.getElementsByName('card')[0].value;
//coefficient 加权系数
var coefficient=[7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2];
//validate 验证码
var validate = [1,0,'X',9,8,7,6,5,4,3,2];
card = card.split('');
if(card.length == 18)
{
var sum = 0;
for(var i=0;i<card.length-1;i++)
{
sum += parseInt(card[i])*parseInt(coefficient[i]);
}
if(card[17] == validate[sum%11])
{
}else
{
}
}else
{
}
}
function submit2()
{
var card = document.getElementsByName('card')[0].value;
//coefficient 加权系数
var coefficient=[7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2];
//validate 验证码
var validate = [1,0,'X',9,8,7,6,5,4,3,2];
card = card.split('');
if(card.length == 17)
{
var sum = 0;
for(var i=0;i<card.length;i++)
{
sum += parseInt(card[i])*parseInt(coefficient[i]);