C# 校验帮助类正则表达式

时间:2021-05-02

一、简介

很多时候我们都需要用到一些验证的方法,有时候需要用正则表达式校验数据时,往往需要到网上找很久,结果找到的还不是很符合自己想要的。所以我把自己整理的校验帮助类分享处理,在这里分享一下,给自己留了个底,也给各位需要的朋友们做个参考

二、校验类片段

2.1 邮箱验证

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #region IsEmail(是否邮箱) /// <summary> /// 是否邮箱 /// </summary> /// <param name="value">邮箱地址</param> /// <param name="isRestrict">是否按严格模式验证</param> /// <returns></returns> public static bool IsEmail(string value, bool isRestrict=false) { if (value.IsEmpty()) { return false; } string pattern = isRestrict ? @"^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$" : @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$"; return value.IsMatch(pattern, RegexOptions.IgnoreCase); } /// <summary> /// 是否存在邮箱 /// </summary> /// <param name="value">值</param> /// <param name="isRestrict">是否按严格模式验证</param> /// <returns></returns> public static bool HasEmail(string value, bool isRestrict = false) { if (value.IsEmpty()) { return false; } string pattern = isRestrict ? @"^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$" : @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$"; return value.IsMatch(pattern, RegexOptions.IgnoreCase); }

2.2 手机号码验证

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 #region IsPhoneNumber(是否合法的手机号码) /// <summary> /// 是否合法的手机号码 /// </summary> /// <param name="value">手机号码</param> /// <returns></returns> public static bool IsPhoneNumber(string value) { if (value.IsEmpty()) { return false; } return value.IsMatch(@"^(0|86|17951)?(13[0-9]|15[012356789]|18[0-9]|14[57]|17[678])[0-9]{8}$"); } #endregion #region IsMobileNumber(是否手机号码) /// <summary> /// 是否手机号码 /// </summary> /// <param name="value">手机号码</param> /// <param name="isRestrict">是否按严格模式验证</param> /// <returns></returns> public static bool IsMobileNumberSimple(string value, bool isRestrict = false) { if (value.IsEmpty()) { return false; } string pattern=isRestrict ? @"^[1][3-8]\d{9}$" : @"^[1]\d{10}$"; return value.IsMatch(pattern); } /// <summary> /// 是否手机号码 /// </summary> /// <param name="value">手机号码</param> /// <returns></returns> public static bool IsMobileNumber(string value) { if (value.IsEmpty()) { return false; } value = value.Trim().Replace("^", "").Replace("$", ""); /** * 手机号码: * 13[0-9], 14[5,7], 15[0, 1, 2, 3, 5, 6, 7, 8, 9], 17[6, 7, 8], 18[0-9], 170[0-9] * 移动号段: 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705 * 联通号段: 130,131,132,155,156,185,186,145,176,1709 * 电信号段: 133,153,180,181,189,177,1700 */ return value.IsMatch(@"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|70)\d{8}$"); } /// <summary> /// 是否存在手机号码 /// </summary> /// <param name="value">值</param> /// <param name="isRestrict">是否按严格模式验证</param> /// <returns></returns> public static bool HasMobileNumberSimple(string value, bool isRestrict = false) { if (value.IsEmpty()) { return false; } string pattern = isRestrict ? @"[1][3-8]\d{9}" : @"[1]\d{10}"; return value.IsMatch(pattern); } #endregion #region IsChinaMobilePhone(是否中国移动号码) /// <summary> /// 是否中国移动号码 /// </summary> /// <param name="value">手机号码</param> /// <returns></returns> public static bool IsChinaMobilePhone(string value) { if (value.IsEmpty()) { return false; } /** * 中国移动:China Mobile * 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705 */ return value.IsMatch(@"(^1(3[4-9]|4[7]|5[0-27-9]|7[8]|8[2-478])\d{8}$)|(^1705\d{7}$)"); } #endregion #region IsChinaUnicomPhone(是否中国联通号码) /// <summary> /// 是否中国联通号码 /// </summary> /// <param name="value">手机号码</param> /// <returns></returns> public static bool IsChinaUnicomPhone(string value) { if (value.IsEmpty()) { return false; } /** * 中国联通:China Unicom * 130,131,132,155,156,185,186,145,176,1709 */ return value.IsMatch(@"(^1(3[0-2]|4[5]|5[56]|7[6]|8[56])\d{8}$)|(^1709\d{7}$)"); } #endregion #region IsChinaTelecomPhone(是否中国电信号码) /// <summary> /// 是否中国电信号码 /// </summary> /// <param name="value">手机号码</param> /// <returns></returns> public static bool IsChinaTelecomPhone(string value) { if (value.IsEmpty()) { return false; } /** * 中国电信:China Telecom * 133,153,180,181,189,177,1700 */ return value.IsMatch(@"(^1(33|53|77|8[019])\d{8}$)|(^1700\d{7}$)"); }

2.3 身份证验证

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #region IsIdCard(是否身份证号码) /// <summary> /// 是否身份证号码 /// </summary> /// <param name="value">身份证</param> /// <returns></returns> public static bool IsIdCard(string value) { if (value.IsEmpty()) { return false; } if (value.Length == 15) { return value.IsMatch(@"^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$"); } return value.Length == 0x12 && value.IsMatch(@"^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$", RegexOptions.IgnoreCase); }

2.4 Base64编码验证

? 1 2 3 4 5 6 7 8 9 10 #region IsBase64String(是否Base64编码) /// <summary> /// 是否Base64编码 /// </summary> /// <param name="value">Base64字符串</param> /// <returns></returns> public static bool IsBase64String(string value) { return value.IsMatch(@"[A-Za-z0-9\+\/\=]"); }

2.5 时间验证

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 #region IsDate(是否日期) /// <summary> /// 是否日期 /// </summary> /// <param name="value">日期字符串</param> /// <param name="isRegex">是否正则验证</param> /// <returns></returns> public static bool IsDate(string value,bool isRegex=false) { if (value.IsEmpty()) { return false; } if (isRegex) { //考虑到4年一度的366天,还有特殊的2月的日期 return value.IsMatch( @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$"); } DateTime minValue; return DateTime.TryParse(value, out minValue); } /// <summary> /// 是否日期 /// </summary> /// <param name="value">日期字符串</param> /// <param name="format">格式化字符串</param> /// <returns></returns> public static bool IsDate(string value, string format) { return IsDate(value, format, null, DateTimeStyles.None); } /// <summary> /// 是否日期 /// </summary> /// <param name="value">日期字符串</param> /// <param name="format">格式化字符串</param> /// <param name="provider">格式化提供者</param> /// <param name="styles">日期格式</param> /// <returns></returns> public static bool IsDate(string value, string format, IFormatProvider provider, DateTimeStyles styles) { if (value.IsEmpty()) { return false; } DateTime minValue; return DateTime.TryParseExact(value, format, provider, styles, out minValue); } #endregion #region IsDateTime(是否有效时间) /// <summary> /// 是否大于最小时间 /// </summary> /// <param name="value">时间</param> /// <param name="min">最小时间</param> /// <returns></returns> public static bool IsDateTimeMin(string value, DateTime min) { if (value.IsEmpty()) { return false; } DateTime dateTime; if (DateTime.TryParse(value, out dateTime)) { if (DateTime.Compare(dateTime, min) >= 0) { return true; } } return false; } /// <summary> /// 是否小于最大时间 /// </summary> /// <param name="value">时间</param> /// <param name="max">最大时间</param> /// <returns></returns> public static bool IsDateTimeMax(string value, DateTime max) { if (value.IsEmpty()) { return false; } DateTime dateTime; if (DateTime.TryParse(value, out dateTime)) { if (DateTime.Compare(max, dateTime) >= 0) { return true; } } return false; }

2.6 Url验证

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 #region IsUrl(是否Url地址) /// <summary> /// 是否Url地址(统一资源定位) /// </summary> /// <param name="value">url地址</param> /// <returns></returns> public static bool IsUrl(string value) { if (value.IsEmpty()) { return false; } return value.IsMatch( @"^(http|https)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{1,10}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$", RegexOptions.IgnoreCase); } #endregion #region IsUri(是否Uri) /// <summary> /// 是否Uri(统一资源标识) /// </summary> /// <param name="value">uri</param> /// <returns></returns> public static bool IsUri(string value) { if (value.IsEmpty()) { return false; } if (value.IndexOf(".", StringComparison.OrdinalIgnoreCase) == -1) { return false; } var schemes = new[] { "file", "ftp", "gopher", "http", "https", "ldap", "mailto", "net.pipe", "net.tcp", "news", "nntp", "telnet", "uuid" }; bool hasValidSchema = false; foreach (string scheme in schemes) { if (hasValidSchema) { continue; } if (value.StartsWith(scheme, StringComparison.OrdinalIgnoreCase)) { hasValidSchema = true; } } if (!hasValidSchema) { value = "http://" + value; } return Uri.IsWellFormedUriString(value, UriKind.Absolute); } #endregion #region IsMainDomain(是否主域名) /// <summary> /// 是否主域名或者www开头的域名 /// </summary> /// <param name="value">url地址</param> /// <returns></returns> public static bool IsMainDomain(string value) { if (value.IsEmpty()) { return false; } return value.IsMatch( @"^http(s)?\://((www.)?[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{1,10}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$"); } #endregion

2.7 基础格式验证

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 #region IsGuid(是否Guid) /// <summary> /// 是否Guid /// </summary> /// <param name="guid">Guid字符串</param> /// <returns></returns> public static bool IsGuid(string guid) { if (guid.IsEmpty()) { return false; } return guid.IsMatch(@"[A-F0-9]{8}(-[A-F0-9]{4}){3}-[A-F0-9]{12}|[A-F0-9]{32}", RegexOptions.IgnoreCase); } #endregion #region IsPositiveInteger(是否大于0的正整数) /// <summary> /// 是否大于0的正整数 /// </summary> /// <param name="value">正整数</param> /// <returns></returns> public static bool IsPositiveInteger(string value) { if (value.IsEmpty()) { return false; } return value.IsMatch(@"^[1-9]+\d*$"); } #endregion #region IsInt32(是否Int32类型) /// <summary> /// 是否Int32类型 /// </summary> /// <param name="value">整数</param> /// <returns></returns> public static bool IsInt32(string value) { if (value.IsEmpty()) { return false; } return value.IsMatch(@"^[0-9]*$"); } #endregion #region IsDouble(是否Double类型,如果带有.默认为1位0) /// <summary> /// 是否Double类型 /// </summary> /// <param name="value">小数</param> /// <returns></returns> public static bool IsDouble(string value) { if (value.IsEmpty()) { return false; } return value.IsMatch(@"^\d[.]?\d?$"); } /// <summary> /// 是否Double类型 /// </summary> /// <param name="value">小数</param> /// <param name="minValue">最小值</param> /// <param name="maxValue">最大值</param> /// <param name="digit">小数位数,如果是0则不检测</param> /// <returns></returns> public static bool IsDouble(string value, double minValue, double maxValue, int digit) { if (value.IsEmpty()) { return false; } string patten = string.Format(@"^\d[.]?\d{0}$", "{0,10}"); if (digit > 0) { patten = string.Format(@"^\d[.]?\d{0}$", "{" + digit + "}"); } if (value.IsMatch(patten)) { double val = Convert.ToDouble(value); if (val >= minValue && val <= maxValue) { return true; } } return false; } #endregion #region IsInteger(是否整数) /// <summary> /// 是否整数 /// </summary> /// <param name="value">值</param> /// <returns>结果</returns> public static bool IsInteger(string value) { if (value.IsEmpty()) { return false; } return value.IsMatch(@"^\-?[0-9]+$"); } #endregion #region IsUnicode(是否Unicode字符串) /// <summary> /// 是否Unicode字符串 /// </summary> /// <param name="value">unicode字符串</param> /// <returns>结果</returns> public static bool IsUnicode(string value) { if (value.IsEmpty()) { return false; } return value.IsMatch( @"^(http|https|ftp|rtsp|mms):(\/\/|\\\\)[A-Za-z0-9%\-_@]+\.[A-Za-z0-9%\-_@]+[A-Za-z0-9\.\/=\?%\-&_~`@:\+!;]*$"); } #endregion #region IsDecimal(是否数字型) /// <summary> /// 是否数字型 /// </summary> /// <param name="value">数字</param> /// <returns></returns> public static bool IsDecimal(string value) { if (value.IsEmpty()) { return false; } return value.IsMatch(@"^([0-9])[0-9]*(\.\w*)?$"); } #endregion

2.8 IP、Mac地址验证

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #region IsMac(是否Mac地址) /// <summary> /// 是否Mac地址 /// </summary> /// <param name="value">Mac地址</param> /// <returns></returns> public static bool IsMac(string value) { if (value.IsEmpty()) { return false; } return value.IsMatch(@"^([0-9

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章