1 public class IDNumber
2 {
3 private IDNumber()
4 { }
5
6 #region 身份证格式验证,以及15.18位互转方法
7 /// <summary>
8 /// 验证18位身份证格式
9 /// </summary>
10 /// <param name="cid"></param>
11 /// <returns>返回字符串,出错信息</returns>
12 public static bool IsCid18(string cid)
13 {
14 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, "国外" };
15 double iSum = 0;
16 System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex(@"^\d{17}(\d|X|x)$");
17 System.Text.RegularExpressions.Match mc = rg.Match(cid);
18 if (!mc.Success)
19 {
20 return false;
21 }
22 cid = cid.ToLower();
23 cid = cid.Replace("x", "a");
24 if (aCity[int.Parse(cid.Substring(0, 2))] == null)
25 {
26 return false;
27 }
28 try
29 {
30 DateTime.Parse(cid.Substring(6, 4) + "-" + cid.Substring(10, 2) + "-" + cid.Substring(12, 2));
31 }
32 catch
33 {
34 return false;
35 }
36 for (int i = 17; i >= 0; i--)
37 {
38 iSum += (System.Math.Pow(2, i) % 11) * int.Parse(cid[17 - i].ToString(), System.Globalization.NumberStyles.HexNumber);
39
40 }
41 if (iSum % 11 != 1)
42 return false;
43
44 return true;
45
46 }
47
48 /// <summary>
49 /// 验证15位身份证格式
50 /// </summary>
51 /// <param name="cid"></param>
52 /// <returns></returns>
53 public static bool IsCid15(string cid)
54 {
55 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, "国外" };
56
57 System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex(@"^\d{15}$");
58 System.Text.RegularExpressions.Match mc = rg.Match(cid);
59 if (!mc.Success)
60 {
61 return false;
62 }
63 cid = cid.ToLower();
64 cid = cid.Replace("x", "a");
65 if (int.Parse(cid.Substring(0, 2)) > aCity.Length)
66 {
67 return false;//非法地区
68 }
69 if (aCity[int.Parse(cid.Substring(0, 2))] == null)
70 {
71 return false;//非法地区
72 }
73 try
74 {
75 DateTime.Parse(cid.Substring(6, 2) + "-" + cid.Substring(8, 2) + "-" + cid.Substring(10, 2));
76 }
77 catch
78 {
79 return false;//非法生日
80 }
81 return true;
82 }
83
84 /// <summary>
85 /// 验证18位身份证格式
86 /// </summary>
87 /// <param name="cid"></param>
88 /// <returns>返回字符串,出错信息</returns>
89 public static string CheckCidInfo18(string cid)
90 {
91 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, "国外" };
92 double iSum = 0;
93 System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex(@"^\d{17}(\d|X|x)$");
94 System.Text.RegularExpressions.Match mc = rg.Match(cid);
95 if (!mc.Success)
96 {
97 return "- 您的身份证号码格式有误!";
98 }
99 cid = cid.ToLower();
100 cid = cid.Replace("x", "a");
101 if (aCity[int.Parse(cid.Substring(0, 2))] == null)
102 {
103 return "- 您的身份证号码格式有误!";//非法地区
104 }
105 try
106 {
107 DateTime.Parse(cid.Substring(6, 4) + "-" + cid.Substring(10, 2) + "-" + cid.Substring(12, 2));
108 }
109 catch
110 {
111 return "- 您的身份证号码格式有误!";//非法生日
112 }
113 for (int i = 17; i >= 0; i--)
114 {
115 iSum += (System.Math.Pow(2, i) % 11) * int.Parse(cid[17 - i].ToString(), System.Globalization.NumberStyles.HexNumber);
116
117 }
118 if (iSum % 11 != 1)
119 return ("- 您的身份证号码格式有误!");//非法证号
120
121 return "";
122
123 }
124
125 /// <summary>
126 /// 验证15位身份证格式
127 /// </summary>
128 /// <param name="cid"></param>
129 /// <returns></returns>
130 public static string CheckCidInfo15(string cid)
131 {
132 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, "国外" };
133
134 System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex(@"^\d{15}$");
135 System.Text.RegularExpressions.Match mc = rg.Match(cid);
136 if (!mc.Success)
137 {
138 return "- 您的身份证号码格式有误!";
139 }
140 cid = cid.ToLower();
141 cid = cid.Replace("x", "a");
142 if (int.Parse(cid.Substring(0, 2)) > aCity.Length)
143 {
144 return "- 您的身份证号码格式有误!";//非法地区
145 }
146 if (aCity[int.Parse(cid.Substring(0, 2))] == null)
147 {
148 return "- 您的身份证号码格式有误!";//非法地区
149 }
150 try
151 {
152 DateTime.Parse(cid.Substring(6, 2) + "-" + cid.Substring(8, 2) + "-" + cid.Substring(10, 2));
153 }
154 catch
155 {
156 return "- 您的身份证号码格式有误!";//非法生日
157 }
158 return "";
159 }
160
161 /// <summary>
162 /// 15位转18位身份证号
163 /// </summary>
164 /// <param name="perIDSrc"></param>
165 /// <returns></returns>
166 public static string per15To18(string perIDSrc)
167 {
168 int iS = 0;
169 //加权因子常数
170 int[] iW = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
171 //校验码常数
172 string LastCode = "10X98765432";
173 //新身份证号
174 string perIDNew;
175
176 perIDNew = perIDSrc.Substring(0, 6);
177 //填在第6位及第7位上填上‘1’,‘9’两个数字
178 perIDNew += "19";
179 perIDNew += perIDSrc.Substring(6, 9);
180 //进行加权求和
181 for (int i = 0; i < 17; i++)
182 {
183 iS += int.Parse(perIDNew.Substring(i, 1)) * iW[i];
184 }
185
186 //取模运算,得到模值
187 int iY = iS % 11;
188 //从LastCode中取得以模为索引号的值,加到身份证的最后一位,即为新身份证号。
189 perIDNew += LastCode.Substring(iY, 1);
190
191 return perIDNew;
192 }
193
194 /// <summary>
195 /// 18位转15位身份证号
196 /// </summary>
197 /// <param name="perIDSrc"></param>
198 /// <returns></returns>
199 public static string per18To15(string perIDSrc)
200 {
201 //前6位
202 string str1 = perIDSrc.Substring(0, 6);
203 //后9位
204 string str2 = perIDSrc.Substring(8, 9);
205 //新字符串
206 string perIDNew = str1 + str2;
207 return perIDNew;
208
209 }
210
211 /// <summary>
212 /// 根据身份证号取得生日及性别
213 /// </summary>
214 /// <param name="idNumber"></param>
215 /// <returns></returns>
216 public static result GetIDNumberInfo(string idNumber)
217 {
218 result r = new result();
219 idNumber = idNumber.Replace(" ", "");
220 if (idNumber.Length == 15)
221 {
222 string a = idNumber.Substring(14, 1);
223 if (int.Parse(idNumber.Substring(14, 1)) % 2 == 0)
224 {
225 r.sex = CommonType.Code.Sex.Female;
226 }
227 else
228 {
229 r.sex = CommonType.Code.Sex.Male;
230 }
231 var year = idNumber.Substring(6, 1) == "0" ? "20" + idNumber.Substring(6, 2) : "19" + idNumber.Substring(6, 2);
232 var month = idNumber.Substring(8, 2);
233 var day = idNumber.Substring(10, 2);
234 r.birthday = DateTime.Parse(year + "-" + month + "-" + day);
235 r.age = (DateTime.Now.Year - r.birthday.Value.Year);
236 }
237 if (idNumber.Length == 18)
238 {
239 if (int.Parse(idNumber.Substring(14, 3)) % 2 == 0)
240 {
241 r.sex = CommonType.Code.Sex.Female;
242 }
243 else
244 {
245 r.sex = CommonType.Code.Sex.Male;
246 }
247 var year = idNumber.Substring(6, 4);
248 var month = idNumber.Substring(10, 2);
249 var day = idNumber.Substring(12, 2);
250 r.birthday = DateTime.Parse(year + "-" + month + "-" + day);
251 r.age = (DateTime.Now.Year - r.birthday.Value.Year);
252 }
253 return r;
254 }
255
256 public static string GetAge(DateTime dtBirthday, DateTime dtNow)
257 {
258
259 string strAge = string.Empty; // 年龄的字符串表示
260 int intYear = 0; // 岁
261 int intMonth = 0; // 月
262 int intDay = 0; // 天
263
264 // 计算天数
265 intDay = dtNow.Day - dtBirthday.Day;
266 if (intDay < 0)
267 {
268 dtNow = dtNow.AddMonths(-1);
269 intDay += DateTime.DaysInMonth(dtNow.Year, dtNow.Month);
270 }
271
272 // 计算月数
273 intMonth = dtNow.Month - dtBirthday.Month;
274 if (intMonth < 0)
275 {
276 intMonth += 12;
277 dtNow = dtNow.AddYears(-1);
278 }
279 // 计算年数
280 intYear = dtNow.Year - dtBirthday.Year;
281
282 // 格式化年龄输出
283 if (intYear >= 1) // 年份输出
284 {
285 strAge = intYear.ToString() + "岁";
286 }
287 if (intMonth > 0 && intYear <= 5) // 五岁以下可以输出月数
288 {
289 strAge += intMonth.ToString() + "月";
290 }
291 if (intDay >= 0 && intYear < 1) // 一岁以下可以输出天数
292 {
293 if (strAge.Length == 0 || intDay > 0)
294 {
295 strAge += intDay.ToString() + "日";
296 }
297 }
298 return strAge;
299 }
300
301 public class result
302 {
303 DateTime? _birthday = null;
304 int _sex = 0;
305 int _age = 0;
306 public DateTime? birthday
307 {
308 get { return _birthday; }
309 set { _birthday = value; }
310 }
311 public int sex
312 {
313 get { return _sex; }
314 set { _sex = value; }
315 }
316 public int age
317 {
318 get { return _age; }
319 set { _age = value; }
320 }
321 }
322 #endregion
323
324 }