身份证验证并计算
public static class IdCardHelper { static readonly int[] Weight = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 }; static readonly char[] VCode = new char[] { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' }; private const string Address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91"; //定义成年人标准 private const int AuditAge = 18; /// <summary> /// 身份证号码检查,返回用户的年龄,生日,性别,验证是否合法等。 /// </summary> /// <param name="idCardNumList"></param> /// <returns></returns> public static List<IdCardUserInfo> CheckIdentityCard(List<string> idCardNumList) { var list = new List<IdCardUserInfo>(); if (idCardNumList == null || idCardNumList.Count <= 0) return list; foreach (var idCardNum in idCardNumList) { var user = new IdCardUserInfo { IdCardNum = idCardNum }; ProcessIdCard(idCardNum, user); list.Add(user); } return list; } static void ProcessIdCard(string idCardNum, IdCardUserInfo user) { try { bool valid; if (idCardNum.Length == 18) { var age = GetAge(idCardNum.Substring(6, 4)); var birthday = idCardNum.Substring(6, 4) + "-" + idCardNum.Substring(10, 2) + "-" + idCardNum.Substring(12, 2); if (!CheckValidCode(idCardNum.Substring(0, 17), idCardNum[17])) valid = false; else if (!CheckACode(idCardNum.Substring(0, 2))) valid = false; else if (age == 0) valid = false; else valid = true; user.IsValid = valid; if (valid) { int g = Convert.ToInt32(idCardNum.Substring(14, 3)); if (g % 2 == 0) { user.Gender = 1; } else { user.Gender = 0; } user.Age = age; user.IsAdult = age >= AuditAge; user.BirthDay = Convert.ToDateTime(birthday); } } else if (idCardNum.Length == 15) { var age = GetAge("19" + idCardNum.Substring(6, 2)); var birthday = "19" + idCardNum.Substring(6, 2) + "-" + idCardNum.Substring(8, 2) + "-" + idCardNum.Substring(10, 2); if (!CheckACode(idCardNum.Substring(0, 2))) valid = false; else if (age == 0) valid = false; else valid = true; user.IsValid = valid; if (valid) { int g = Convert.ToInt32(idCardNum.Substring(12, 3)); if (g % 2 == 0) { user.Gender = 1; } else { user.Gender = 0; } user.Age = age; user.IsAdult = age >= AuditAge; user.BirthDay = Convert.ToDateTime(birthday); } } else { user.IsValid = false; } } catch (Exception ex) { user.IsValid = false; } } static bool CheckValidCode(string input17, char last)//检查最后一位校验码 { if (last == CheckValidCode(input17)) return true; return false; } static char CheckValidCode(string input17)//检查最后一位校验码 { int sum = 0, cur; for (int i = 0; i < 17; i++) { cur = Convert.ToInt32(input17[i]) - 48; sum += cur * Weight[i]; } return VCode[(sum % 11)]; } static int GetAge(string input4)//算出年龄 { try { return Convert.ToInt32(DateTime.Now.Year) - Convert.ToInt32(input4); } catch { return 0; } } static bool CheckACode(string input2)//检查地区码 { if (Address.IndexOf(input2) != -1) return true; return false; } } public class IdCardUserInfo { /// <summary> /// 身份证号码 /// </summary> public string IdCardNum { get; set; } /// <summary> /// 性别 0男1女 /// </summary> public int Gender { get; set; } /// <summary> /// 生日 /// </summary> public DateTime BirthDay { get; set; } /// <summary> /// 年龄 /// </summary> public int Age { get; set; } /// <summary> /// 身份证是否合法 /// </summary> public bool IsValid { get; set; } /// <summary> /// 是否是成年人 /// </summary> public bool IsAdult { get; set; } }