正則表示式驗證類
阿新 • • 發佈:2020-12-07
/// <summary> /// 正則表示式驗證類 /// </summary> public static class RegexHelper { /// <summary> /// 是否為郵箱地址 /// </summary> /// <param name="str"></param> /// <returns></returns> public static bool IsEmail(string str) {var tmpRegex = new System.Text.RegularExpressions.Regex(@"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$"); return tmpRegex.IsMatch(str); } /// <summary> /// 驗證日期 /// </summary> /// <param name="source"></param> /// <returns></returns>public static bool IsDateTime(string source) { try { var time = Convert.ToDateTime(source); return true; } catch { return false; } } /// <summary>/// 驗證手機號 /// </summary> /// <param name="source"></param> /// <returns></returns> public static bool IsMobile(string source) { return Regex.IsMatch(source, @"^1[3456789]\d{9}$", RegexOptions.IgnoreCase); } /// <summary> /// 驗證身份證是否有效 /// </summary> /// <param name="Id"></param> /// <returns></returns> public static bool IsIDCard(string Id) { if (Id.Length == 18) { var check = IsIdCard18(Id); return check; } else if (Id.Length == 15) { var check = IsIdCard15(Id); return check; } else { return false; } } /// <summary> /// 驗證18位身份證號 /// </summary> /// <param name="Id"></param> /// <returns></returns> public static bool IsIdCard18(string Id) { long n = 0; if (long.TryParse(Id.Remove(17), out n) == false || n < Math.Pow(10, 16) || long.TryParse(Id.Replace('x', '0').Replace('X', '0'), out n) == false) { return false; //數字驗證 } var address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91"; if (address.IndexOf(Id.Remove(2), System.StringComparison.Ordinal) == -1) { return false; //省份驗證 } var birth = Id.Substring(6, 8).Insert(6, "-").Insert(4, "-"); var time = new DateTime(); if (DateTime.TryParse(birth, out time) == false) { return false; //生日驗證 } var arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(','); var Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(','); var Ai = Id.Remove(17).ToCharArray(); var sum = 0; for (var i = 0; i < 17; i++) { sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString()); } var y = -1; Math.DivRem(sum, 11, out y); if (arrVarifyCode[y] != Id.Substring(17, 1).ToLower()) { return false; //校驗碼驗證 } return true; //符合GB11643-1999標準 } /// <summary> /// 驗證15位身份證號 /// </summary> /// <param name="Id"></param> /// <returns></returns> public static bool IsIdCard15(string Id) { long n = 0; if (long.TryParse(Id, out n) == false || n < Math.Pow(10, 14)) { return false; //數字驗證 } var address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91"; if (address.IndexOf(Id.Remove(2), System.StringComparison.Ordinal) == -1) { return false; //省份驗證 } var birth = Id.Substring(6, 6).Insert(4, "-").Insert(2, "-"); var time = new DateTime(); if (DateTime.TryParse(birth, out time) == false) { return false; //生日驗證 } return true; //符合15位身份證標準 } /// <summary> /// 是不是中國電話,格式010-85849685 /// </summary> /// <param name="source"></param> /// <returns></returns> public static bool IsTel(string source) { return Regex.IsMatch(source, @"^\d{3,4}-?\d{6,8}$", RegexOptions.IgnoreCase); } }