C#身份证验证小例子
投稿:shangke
C#身份证验证小例子,需要的朋友可以参考一下
复制代码 代码如下:
private string CheckCidInfo(string cid)
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 ? "男" : "女"));
}