C#身份证验证小例子
时间:2022-06-22 05:22:01
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 ? "男" : "女"));
}
标签:身份证,验证
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java并发线程池实例分析讲解
2022-08-05 20:25:40
![](https://img.aspxhome.com/file/2023/2/61382_0s.png)
Android开发中ProgressDialog简单用法示例
2021-11-03 10:17:37
c# List find()方法返回值的问题说明(返回结果为对象的指针)
2023-11-20 21:55:34
![](https://img.aspxhome.com/file/2023/7/74817_0s.png)
Kotlin 基础教程之泛型
2022-05-04 03:35:12
Linux下用java -jar运行可执行jar包的方法教程
2022-11-24 18:01:46
软件开发七大过程模型
2023-12-22 17:06:14
![](https://img.aspxhome.com/file/2023/2/68722_0s.jpg)
基于C#实现一个简单的FTP操作工具
2023-05-12 11:50:58
SSM项目使用拦截器实现登录验证功能
2023-06-17 16:12:38
![](https://img.aspxhome.com/file/2023/0/65600_0s.png)
EditText监听方法,实时的判断输入多少字符
2021-08-16 11:32:36
![](https://img.aspxhome.com/file/2023/1/94981_0s.jpg)
如何用C#找出数组中只出现了一次的数字
2021-06-15 15:42:56
Java判断变量是否为空问题的方法总结
2023-02-03 12:23:09
Java必备知识之位运算及常见进制解读
2022-02-10 00:42:19
![](https://img.aspxhome.com/file/2023/5/92715_0s.jpg)
详谈java 堆区、方法区和栈区
2023-11-23 18:35:22
JAVA中的Token 基于Token的身份验证实例
2023-11-09 18:05:09
Java线程同步Lock同步锁代码示例
2023-11-17 13:23:54
spring中的FactoryBean代码示例
2023-11-11 18:37:20
Java7和Java8中的ConcurrentHashMap原理解析
2022-06-09 22:06:28
![](https://img.aspxhome.com/file/2023/7/66947_0s.jpg)
Java使用动态规划算法思想解决背包问题
2022-12-02 03:53:49
![](https://img.aspxhome.com/file/2023/7/89497_0s.png)
c# socket编程udp客户端实现代码分享
2023-06-16 05:03:31
C# WinForm实现图片浏览器
2022-04-12 23:28:00
![](https://img.aspxhome.com/file/2023/6/92906_0s.jpg)