(3)通过模得到对应的校验码
Y: 0 1 2 3 4 5 6 7 8 9 10
校验码: 1 0 X 9 8 7 6 5 4 3 2
四、举例如下:
北京市朝阳区: 11010519491231002X
广东省汕头市: 440524188001010014
以下是程序代码:
private string CheckCidInfo(string cid)
{
string[] aCity = new string[]{null,null,null,null,null,null,null,null,null,null,null,"北京","天津","河北","山西","内蒙古",null,null,null,null,null,"辽宁","吉林","黑龙江",null,null,null,null,null,null,null,"上海","江苏","浙江","安微","福建","江西","山东",null,null,null,"河南","湖北","湖南","广东","广西","海南",null,null,null,"重庆","四川","贵州","云南","西藏",null,null,null,null,null,null,"陕西","甘肃","青海","宁夏","新疆",null,null,null,null,null,"台湾",null,null,null,null,null,null,null,null,null,"香港","澳门",null,null,null,null,null,null,null,null,"国外"};
double iSum=0;
string info="";
System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex(@"^\d{17}(\d|x)$");
System.Text.RegularExpressions.Match mc = rg.Match(cid);
if(!mc.Success)
{
return "";
}
cid = cid.ToLower();
cid = cid.Replace("x","a");
if(aCity[int.Parse(cid.Substring(0,2))]==null)
{
return "非法地区";
}
try
{
DateTime.Parse(cid.Substring(6,4)+"-"+cid.Substring(10,2)+"-"+cid.Substring(12,2));
}
catch
{
return "非法生日";
}
for(int i=17;i>=0;i--)
{
iSum +=(System.Math.Pow(2,i)%11)*int.Parse(cid[17-i].ToString(),System.Globalization.NumberStyles.HexNumber);
}
if(iSum%11!=1)
return("非法证号");
return(aCity[int.Parse(cid.Substring(0,2))]+","+cid.Substring(6,4)+"-"+cid.Substring(10,2)+"-"+cid.Substring(12,2)+","+(int.Parse(cid.Substring(16,1))%2==1?"男":"女"));
}
调用测试:
Response.Write(CheckCidInfo("340524198002300019"));
Response.Write(CheckCidInfo("34052419800101001x"));
以下是js的验证
<html>
<head>
<title>身份证号是否有效验证</title>
</head>
<body>
请输入身份证号码:<input type="text" size="20" id="asidinput" />
<input type="button" value="验证" onclick="alert(cidInfo(asidinput.value))" />
<script>
var aCity={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"}
function cidInfo(sId)
{
var iSum=0
var info=""
if(!/^\d{17}(\d|x)$/i.test(sId))
{
return false;
}
sId=sId.replace(/x$/i,"a");
if(aCity[parseInt(sId.substr(0,2))]==null)
{
return false;
}
sBirthday=sId.substr(6,4)+"-"+Number(sId.substr(10,2))+"-"+Number(sId.substr(12,2));
var d=new Date(sBirthday.replace(/-/g,"/"))
if(sBirthday!=(d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate()))
{
return false;
}
for(var i = 17;i>=0;i --)
{
iSum += (Math.pow(2,i) % 11) * parseInt(sId.charAt(17 - i),11)
}
if(iSum%11!=1)
{
return false;
}
return true;
}
</script>
</body>
</html>