/// <summary> /// 判定是否为有效身份证号码 /// </summary> /// <param name="source">身份证号</param> /// <returns>是否为有效身份证号</returns> public static bool isIDCardNumber(string source) { if (source.Trim().Length == 18) { char[] chars = source.Trim().ToCharArray(); int[] bits = new int[17]; try { for (int i = 0; i < 17; i++) { bits[i] = Convert.ToInt32(chars[i].ToString()); } } catch { return(false); } int total = 0; for (int i = 0; i < 17; i++) { total += bits[i] * IDCARD_BITS_FACTOR[i]; } int rear = total % 11; string rearstring = IDCARD_VERIFY_FACTOR[rear].ToString(); if (rearstring == "10") { rearstring = "X"; } try { if (chars[17].ToString().ToUpper() == rearstring) { return(true); } } catch { return(false); } } if (source.Trim().Length == 15) { char[] chars = source.Trim().ToCharArray(); int[] bits = new int[15]; try { for (int i = 0; i < 15; i++) { bits[i] = Convert.ToInt32(chars[i].ToString()); } } catch { return(false); } string date = "19" + chars[6] + chars[7] + "-" + chars[8] + chars[9] + "-" + chars[10] + chars[11]; try { DateTime birth = Convert.ToDateTime(date); } catch { return(false); } string code = bits[0].ToString() + bits[1] + bits[2] + bits[3] + bits[4] + bits[5]; return(AreaUtil.isValidAreaCode(code)); } return(false); }
public static bool isValidAreaCode(string area_code) { Province province = null; City city = null; District district = null; AreaUtil.areaByCode(area_code, out province, out city, out district); if (province.id > 0 && city.id > 0 && district.id > 0 && district.id != 999999) { return(true); } return(false); }