中国身份证号码验证,支持15,18位,可验证成功90%的身份证号
支持15,18位,可验证成功90%的身份证号码
1using System;
2
3/**//// <summary>
4/// Summary description for IDCardValid
5/// </summary>
6public class IDCardValid
7{
8 public IDCardValid()
9 {
10 //
11 // TODO: Add constructor logic here
12 //
13 }
14
15 /**//// <summary>
16 /// 验证身份证号码
17 /// </summary>
18 /// <param name=”Id”>身份证号码</param>
19 /// <returns>验证成功为True,否则为False</returns>
20 public static bool CheckIDCard(string Id)
21 {
22 if (Id.Length == 18)
23 {
24 bool check = CheckIDCard18(Id);
25 return check;
26 }
27 else if (Id.Length == 15)
28 {
29 bool check = CheckIDCard15(Id);
30 return check;
31 }
32 else
33 {
34 return false;
35 }
36 }
37
38 身份证号码验证#region 身份证号码验证
39
40 /**//// <summary>
41 /// 验证15位身份证号
42 /// </summary>
43 /// <param name=”Id”>身份证号</param>
44 /// <returns>验证成功为True,否则为False</returns>
45 private static bool CheckIDCard18(string Id)
46 {
47 long n = 0;
48 if (long.TryParse(Id.Remove(17), out n) == false || n < Math.Pow(10, 16) || long.TryParse(Id.Replace(\’x\’, \’0\’).Replace(\’X\’, \’0\’), out n) == false)
49 {
50 return false;//数字验证
51 }
52 string address = “11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91“;
53 if (address.IndexOf(Id.Remove(2)) == –1)
54 {
55 return false;//省份验证
56 }
57 string birth = Id.Substring(6, 8).Insert(6, “–“).Insert(4, “–“);
58 DateTime time = new DateTime();
59 if (DateTime.TryParse(birth, out time) == false)
60 {
61 return false;//生日验证
62 }
63 string[] arrVarifyCode = (“1,0,x,9,8,7,6,5,4,3,2“).Split(\’,\’);
64 string[] Wi = (“7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2“).Split(\’,\’);
65 char[] Ai = Id.Remove(17).ToCharArray();
66 int sum = 0;
67 for (int i = 0; i < 17; i++)
68 {
69 sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());
70 }
71 int y = –1;
72 Math.DivRem(sum, 11, out y);
73 if (arrVarifyCode[y] != Id.Substring(17, 1).ToLower())
74 {
75 return false;//校验码验证
76 }
77 return true;//符合GB11643-1999标准
78 }
79
80 /**//// <summary>
81 /// 验证18位身份证号
82 /// </summary>
83 /// <param name=”Id”>身份证号</param>
84 /// <returns>验证成功为True,否则为False</returns>
85 private static bool CheckIDCard15(string Id)
86 {
87 long n = 0;
88 if (long.TryParse(Id, out n) == false || n < Math.Pow(10, 14))
89 {
90 return false;//数字验证
91 }
92 string address = “11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91“;
93 if (address.IndexOf(Id.Remove(2)) == –1)
94 {
95 return false;//省份验证
96 }
97 string birth = Id.Substring(6, 6).Insert(4, “–“).Insert(2, “–“);
98 DateTime time = new DateTime();
99 if (DateTime.TryParse(birth, out time) == false)
100 {
101 return false;//生日验证
102 }
103 return true;//符合15位身份证标准
104 }
105 #endregion
106}
107
2
3/**//// <summary>
4/// Summary description for IDCardValid
5/// </summary>
6public class IDCardValid
7{
8 public IDCardValid()
9 {
10 //
11 // TODO: Add constructor logic here
12 //
13 }
14
15 /**//// <summary>
16 /// 验证身份证号码
17 /// </summary>
18 /// <param name=”Id”>身份证号码</param>
19 /// <returns>验证成功为True,否则为False</returns>
20 public static bool CheckIDCard(string Id)
21 {
22 if (Id.Length == 18)
23 {
24 bool check = CheckIDCard18(Id);
25 return check;
26 }
27 else if (Id.Length == 15)
28 {
29 bool check = CheckIDCard15(Id);
30 return check;
31 }
32 else
33 {
34 return false;
35 }
36 }
37
38 身份证号码验证#region 身份证号码验证
39
40 /**//// <summary>
41 /// 验证15位身份证号
42 /// </summary>
43 /// <param name=”Id”>身份证号</param>
44 /// <returns>验证成功为True,否则为False</returns>
45 private static bool CheckIDCard18(string Id)
46 {
47 long n = 0;
48 if (long.TryParse(Id.Remove(17), out n) == false || n < Math.Pow(10, 16) || long.TryParse(Id.Replace(\’x\’, \’0\’).Replace(\’X\’, \’0\’), out n) == false)
49 {
50 return false;//数字验证
51 }
52 string address = “11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91“;
53 if (address.IndexOf(Id.Remove(2)) == –1)
54 {
55 return false;//省份验证
56 }
57 string birth = Id.Substring(6, 8).Insert(6, “–“).Insert(4, “–“);
58 DateTime time = new DateTime();
59 if (DateTime.TryParse(birth, out time) == false)
60 {
61 return false;//生日验证
62 }
63 string[] arrVarifyCode = (“1,0,x,9,8,7,6,5,4,3,2“).Split(\’,\’);
64 string[] Wi = (“7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2“).Split(\’,\’);
65 char[] Ai = Id.Remove(17).ToCharArray();
66 int sum = 0;
67 for (int i = 0; i < 17; i++)
68 {
69 sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());
70 }
71 int y = –1;
72 Math.DivRem(sum, 11, out y);
73 if (arrVarifyCode[y] != Id.Substring(17, 1).ToLower())
74 {
75 return false;//校验码验证
76 }
77 return true;//符合GB11643-1999标准
78 }
79
80 /**//// <summary>
81 /// 验证18位身份证号
82 /// </summary>
83 /// <param name=”Id”>身份证号</param>
84 /// <returns>验证成功为True,否则为False</returns>
85 private static bool CheckIDCard15(string Id)
86 {
87 long n = 0;
88 if (long.TryParse(Id, out n) == false || n < Math.Pow(10, 14))
89 {
90 return false;//数字验证
91 }
92 string address = “11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91“;
93 if (address.IndexOf(Id.Remove(2)) == –1)
94 {
95 return false;//省份验证
96 }
97 string birth = Id.Substring(6, 6).Insert(4, “–“).Insert(2, “–“);
98 DateTime time = new DateTime();
99 if (DateTime.TryParse(birth, out time) == false)
100 {
101 return false;//生日验证
102 }
103 return true;//符合15位身份证标准
104 }
105 #endregion
106}
107
源代码演示下载:
http://files.cnblogs.com/moozi/IDCardValid.rar
版权声明:本文为moozi原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。