教程集 www.jiaochengji.com
教程集 >  脚本编程  >  Asp.net  >  正文 C#身份证验证的代码一例

C#身份证验证的代码一例

发布时间:2016-02-25   编辑:jiaochengji.com
为大家介绍一个c#实现验证身份证号码的代码,例子比较简单,但可以完成身份证号码的验证功能,用于学习还是不错的。

代码如下:

///验证身份证
///by http://www.jbxue.com
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 ? "男" : "女"));
}

您可能感兴趣的文章:
常用js验证代码大全(Email、手机号码、身份证号码、文件类型等)
php验证身份证号码正确性的示例代码
js身份证验证代码 支持15位和18位身份证
asp.net 验证身份证号码的代码一例
PHP验证身份证格式
PHP 验证身份证号的函数
JS验证身份证有效性的实例代码
js根据身份证号码计算性别、年龄、生日的示例代码
js 验证身份证信息有效性实例
php验证码大全(实例分享)

[关闭]
~ ~