支持15,18位,可验证成功90%的身份证号码
1
using System;
2![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
4
/// Summary description for IDCardValid
5
/// </summary>
6
public class IDCardValid
7![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
8
public IDCardValid()
9![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
10
//
11
// TODO: Add constructor logic here
12
//
13
}
14![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
16
/// 验证身份证号码
17
/// </summary>
18
/// <param name="Id">身份证号码</param>
19
/// <returns>验证成功为True,否则为False</returns>
20
public static bool CheckIDCard(string Id)
21![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
22
if (Id.Length == 18)
23![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
24
bool check = CheckIDCard18(Id);
25
return check;
26
}
27
else if (Id.Length == 15)
28![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
29
bool check = CheckIDCard15(Id);
30
return check;
31
}
32
else
33![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
34
return false;
35
}
36
}
37![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
身份证号码验证#region 身份证号码验证
39![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
41
/// 验证15位身份证号
42
/// </summary>
43
/// <param name="Id">身份证号</param>
44
/// <returns>验证成功为True,否则为False</returns>
45
private static bool CheckIDCard18(string Id)
46![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
50
return false;//数字验证
51
}
52
string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
53
if (address.IndexOf(Id.Remove(2)) == -1)
54![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
75
return false;//校验码验证
76
}
77
return true;//符合GB11643-1999标准
78
}
79![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
80![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
81
/// 验证18位身份证号
82
/// </summary>
83
/// <param name="Id">身份证号</param>
84
/// <returns>验证成功为True,否则为False</returns>
85
private static bool CheckIDCard15(string Id)
86![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
87
long n = 0;
88
if (long.TryParse(Id, out n) == false || n < Math.Pow(10, 14))
89![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
90
return false;//数字验证
91
}
92
string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
93
if (address.IndexOf(Id.Remove(2)) == -1)
94![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
101
return false;//生日验证
102
}
103
return true;//符合15位身份证标准
104
}
105
#endregion
106
}
107![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
源代码演示下载:
https://files.cnblogs.com/moozi/IDCardValid.rar
posted @
2008-04-07 09:24
木子博客
阅读(
3019)
评论()
编辑
收藏
举报