asp.net身份证号码验证(转载)

 

代码
1 private string GetCard(string cid)
2 {
3 if (cid.Length == 15)
4 {
5 cid = per15To18(cid);
6 }
7 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, "国外" };
8 double iSum = 0;
9 //string info="";
10   System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex(@"^\d{17}(\d|x)$");
11 System.Text.RegularExpressions.Match mc = rg.Match(cid);
12 if (!mc.Success)
13 {
14 return "格式不正确!";
15 //return "";
16   }
17 cid = cid.ToLower();
18 cid = cid.Replace("x", "a");
19 if (aCity[int.Parse(cid.Substring(0, 2))] == null)
20 {
21 return "非法地区";
22 //return "";
23 }
24 try
25 {
26 DateTime.Parse(cid.Substring(6, 4) + "-" + cid.Substring(10, 2) + "-" + cid.Substring(12, 2));
27 }
28 catch
29 {
30 return "非法生日";
31 //return "";
32 }
33 for (int i = 17; i >= 0; i--)
34 {
35 iSum += (System.Math.Pow(2, i) % 11) * int.Parse(cid[17 - i].ToString(), System.Globalization.NumberStyles.HexNumber);
36 }
37 if (iSum % 11 != 1)
38 return("非法证号");
39 //return ("");
40 return (cid.Substring(6, 4) + "-" + cid.Substring(10, 2) + "-" + cid.Substring(12, 2));
41 }
42
43 private string per15To18(string perIDSrc)
44 {
45 int iS = 0;
46 //加权因子常数
47 int[] iW = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
48 //校验码常数
49 string LastCode = "10X98765432";
50 //新身份证号
51 string perIDNew;
52 perIDNew = perIDSrc.Substring(0, 6);
53 //填在第6位及第7位上填上‘1’,‘9’两个数字
54 perIDNew += "19";
55 perIDNew += perIDSrc.Substring(6, 9);
56 //进行加权求和
57 for (int i = 0; i < 17; i++)
58 {
59 iS += int.Parse(perIDNew.Substring(i, 1)) * iW[i];
60 }
61 //取模运算,得到模值
62 int iY = iS % 11;
63 //从LastCode中取得以模为索引号的值,加到身份证的最后一位,即为新身份证号。
64 perIDNew += LastCode.Substring(iY, 1);
65 return perIDNew;
66 }
67 //取得性别
68 private int CheckSex(string cid)
69 {
70 int sex;
71 int man;
72 if (cid.Length == 15)
73 {
74 man = int.Parse(cid.Substring(14, 1));
75 }
76 else if (cid.Length == 18)
77 {
78 man = int.Parse(cid.Substring(16, 1));
79 }
80 else man = 1;
81 if (man % 2 == 0) sex = 0;
82 else sex = 1;
83 return sex;
84 }
85 //实际运用
86 int sex = CheckSex(IdCard);//性别
87 string addr = aCity[int.Parse(Business.DLL.Globals.stringLen(IdCard, 0, 2))];//地址
88 string aa = GetCard(IdCard);//得到生日并且在下面判断
89 try
90 {
91 Convert.ToDateTime(aa);
92 }
93 catch (Exception)
94 {
95 ph.PageOutBlock("身份证号码有错误的地方", -1);//个人操作类,大家按自己情况修改
96 }
97 DateTime MemberBirthday =Convert.ToDateTime( GetCard(IdCard));//会员生日
98 TimeSpan tm = (TimeSpan)DateTime.Now.Subtract(MemberBirthday);//会员年龄
99 int age = (int)(tm.Days)/365;//年龄取得整

 

posted @ 2010-06-26 11:38  零纪录  阅读(2924)  评论(2编辑  收藏  举报