身份证号码验证帮助类(公共方法)
1 /// <summary> 2 /// 身份证帮助类 3 /// </summary> 4 public class IDCardHelper 5 { 6 #region 检测身份证的合法性 7 public static bool Check(string cardId) 8 { 9 string num = cardId.ToUpper(); 10 int[] factorArr = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 }; 11 char[] parityBit = new char[] { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' }; 12 char[] varArray = new char[18]; 13 var lngProduct = 0; 14 int intCheckDigit = 0; 15 var intStrLen = num.Length; 16 var idNumber = num; 17 if ((intStrLen != 15) && (intStrLen != 18)) 18 { 19 return false; 20 } 21 22 for (int i = 0; i < intStrLen; i++) 23 { 24 varArray[i] = (char)Convert.ToInt32(idNumber[i]); 25 if (((varArray[i] - 48) < 0 || (varArray[i] - 48) > 9) && (i != 17)) 26 { 27 return false; 28 } 29 else if (i < 17) 30 { 31 varArray[i] = (char)((varArray[i] - 48) * factorArr[i]); 32 } 33 } 34 if (intStrLen == 18) 35 { 36 var date8 = idNumber.Substring(6, 8); 37 if (isDate8(date8) == false) 38 { 39 return false; 40 } 41 for (int i = 0; i < 17; i++) 42 { 43 lngProduct = lngProduct + varArray[i]; 44 } 45 intCheckDigit = parityBit[lngProduct % 11]; 46 if (varArray[17] != intCheckDigit) 47 { 48 return false; 49 } 50 } 51 else 52 { 53 var date6 = idNumber.Substring(6, 6); 54 if (isDate6(date6) == false) 55 { 56 return false; 57 } 58 } 59 return true; 60 } 61 #endregion 62 63 #region 位身份证日期检测--年/月--6位 64 /// <summary> 65 /// 15位身份证日期检测--年/月--6位 66 /// </summary> 67 /// <param name="sDate"></param> 68 /// <returns></returns> 69 static bool isDate6(string sDate) 70 { 71 if (!System.Text.RegularExpressions.Regex.IsMatch(sDate, "^[0-9]{6}$")) 72 { 73 return false; 74 } 75 int year = Convert.ToInt32(sDate.Substring(0, 4)); 76 int month = Convert.ToInt32(sDate.Substring(4, 2)); 77 if (year < 1700 || year > 2500) return false; 78 if (month < 1 || month > 12) return false; 79 return true; 80 } 81 #endregion 82 83 #region 18位身份证日期检测--年/月/日--8位 84 /// <summary> 85 /// 18位身份证日期检测--年/月/日--8位 86 /// </summary> 87 /// <param name="sDate"></param> 88 /// <returns></returns> 89 static bool isDate8(string sDate) 90 { 91 if (!System.Text.RegularExpressions.Regex.IsMatch(sDate, "^[0-9]{8}$")) 92 { 93 return false; 94 } 95 int year = Convert.ToInt32(sDate.Substring(0, 4)); 96 int month = Convert.ToInt32(sDate.Substring(4, 2)); 97 int day = Convert.ToInt32(sDate.Substring(6, 2)); 98 99 int[] iaMonthDays = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 100 if (year < 1700 || year > 2500) return false; 101 if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) iaMonthDays[1] = 29; 102 if (month < 1 || month > 12) return false; 103 if (day < 1 || day > iaMonthDays[month - 1]) return false; 104 return true; 105 } 106 #endregion 107 108 #region 根据身份证号码获取年龄、生日 109 public static DateTime? GetBirthday(string cardId) 110 { 111 bool result = Check(cardId); 112 if (result) 113 { 114 int year = Convert.ToInt32(cardId.Substring(6, 4)); 115 int month = Convert.ToInt32(cardId.Substring(10, 2)); 116 int day = Convert.ToInt32(cardId.Substring(12, 2)); 117 return new DateTime(year, month, day); 118 } 119 return null; 120 } 121 #endregion 122 }