项目中需要实现二代身份证号码转一代身份证的功能,在晚上查找了下,网上多是用sql实现,于是参考其算法,用javascript实现。并在html页面中进行了测试,下面将页面代码直接贴出来,供大家参考。
<html>
<script language='javascript'>
function ID18(sourceid)
{
var strTemp;
var ID18;
if(sourceid.length==15)
strTemp = sourceid.substr(0,6)+"19"+ sourceid.substr(6);
else
{
ID18 = sourceid;
return ID18;
}
//alert(strTemp);
ID18 = strTemp + calcVerify(strTemp);
return ID18;
}
function ConvertTo18(str)
{
// var sourceId = //frm.document[0].frm.GetElementById("source").value;
var sourceId = document.getElementById("source").value;
//alert(sourceId);
var a = ID18(sourceId);
document.getElementById("target").value = a;
}
function calcVerify(sCode17)
{
var strVerify;
var intWeight;
var Verify;
strVerify = new Array("1","0","X","9","8","7","6","5","4","3","2");
intWeight = new Array(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1);
var intTemp=0;
var i;
for(i=0;i<=sCode17.length-1;i++)
{
//alert(parseInt(parseInt(sCode17.substr(i,1))*intWeight[i]));
intTemp = parseInt(intTemp) + parseInt(parseInt(sCode17.substr(i,1))*intWeight[i]);
//alert(intTemp);
}
intTemp = intTemp%11;
Verify = strVerify[intTemp];
return Verify;
}
</script>
<body>
<form name="frm" action="">
<table>
<tr>
<td>
<input type="text" name="Id15" value="" id="source">
</td>
<td>
转换结果:
</td>
<td>
<input type="text" name="Id15" value="" id="target" readonly="true">
</td>
</tr>
<tr>
<td colspan="3">
<input type="button" name="转换" value="转换" OnClick="ConvertTo18()">
</td>
</tr>
</table>
</form>
</body>
</html>