身份證號幫助類
阿新 • • 發佈:2020-07-01
public class IdCardHelper { /// <summary> /// 驗證身份證結果 /// </summary> /// <param name="idcard">身份證號碼</param> /// <param name="msg"></param> /// <returns>正確的時候返回string.Empty</returns> public static bool Validate(stringidcard, out string msg) { msg = ""; if (idcard.Length != 18 && idcard.Length != 15) { msg = "身份證號碼必須是15或者18位!"; return false; } if (idcard.Length == 15) { idcard = IdCard15To18(idcard); }var rg = new Regex(@"^\d{17}(\d|X)$"); if (!rg.Match(idcard).Success) { msg = "身份證號碼必須為數字或者X!"; return false; } int llSum = 0, tmp = 0; for (int i = 0; i < 17; i++) { tmp = int.Parse(idcard.Substring(i, 1)); llSum += tmp * li_quan[i]; } llSum = llSum % 11; if (idcard.Substring(17, 1) != ls_jy[llSum]) { msg = "身份證號碼最後一位應該是" + ls_jy[llSum] + "!"; return false; } try { DateTime.Parse(idcard.Substring(6, 4) + "-" + idcard.Substring(10, 2) + "-" + idcard.Substring(12, 2)); } catch { msg = "非法生日"; return false; } return true; } private static string[] ls_jy = { "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2" }; private static int[] li_quan = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 }; /// <summary> /// 15位身份證明號碼轉化成18位用來編碼 /// </summary> /// <param name="idcard">15位的身份證號碼</param> /// <returns></returns> public static string IdCard15To18(string idcard) { if (idcard == null || idcard.Length != 15) { return idcard; } else { string result = string.Empty; int ll_sum = 0, tmp = 0; result = idcard.Substring(0, 6) + "19" + idcard.Substring(6, 9); for (int i = 0; i < 17; i++) { tmp = int.Parse(result.Substring(i, 1)); ll_sum += tmp * li_quan[i]; } ll_sum = ll_sum % 11; result += ls_jy[ll_sum]; return result; } } /// <summary> /// 獲取身份證對應省份的區劃 /// </summary> /// <param name="id">身份證</param> /// <returns>頭兩位+4個0</returns> public static string GetProvince(string id) { return id.Substring(0, 2) + "0000"; } /// <summary> /// 獲取身份證對應縣市的區劃 /// </summary> /// <param name="id">身份證</param> /// <returns>頭4位+2個0</returns> public static string GetCity(string id) { return id.Substring(0, 4) + "00"; } /// <summary> /// 獲取身份證對應地區的區劃 /// </summary> /// <param name="id">身份證</param> /// <returns>頭6位</returns> public static string GetArea(string id) { return id.Substring(0, 6); } /// <summary> /// 根據身份證判斷是否男女 /// </summary> /// <param name="id">身份證號碼</param> /// <returns>返回"男"或者"女"</returns> public static string GetSexName(string id) { int sexStr = 0; if (id.Length == 15) { sexStr = Convert.ToInt32(id.Substring(14, 1)); } else if (id.Length == 18) { sexStr = Convert.ToInt32(id.Substring(16, 1)); } else { throw new ArgumentException("身份證號碼不是15或者18位!"); } return sexStr % 2 == 0 ? "女" : "男"; } /// <summary> /// 根據身份證獲取出生年月 /// </summary> /// <param name="id">身份證號碼</param> /// <returns>出生年月</returns> public static DateTime GetBirthday(string id) { string result = string.Empty; if (id.Length == 15) { result = "19" + id.Substring(6, 2) + "-" + id.Substring(8, 2) + "-" + id.Substring(10, 2); } else if (id.Length == 18) { result = id.Substring(6, 4) + "-" + id.Substring(10, 2) + "-" + id.Substring(12, 2); } else { throw new ArgumentException("身份證號碼不是15或者18位!"); } return Convert.ToDateTime(result); } }