C#身份证验证小例子
程序员文章站
2024-02-11 20:30:28
复制代码 代码如下: private string checkcidinfo(string cid) &...
复制代码 代码如下:
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;
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 ? "男" : "女"));
}
上一篇: C#读取XML中元素和属性值的实现代码