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

#region IsIdCard(是否身份证号码) /// <summary> /// 是否身份证号码 /// </summary> /// <param>身份证</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编码验证

#region IsBase64String(是否Base64编码) /// <summary> /// 是否Base64编码 /// </summary> /// <param>Base64字符串</param> /// <returns></returns> public static bool IsBase64String(string value) { return value.IsMatch(@"[A-Za-z0-9\+\/\=]"); }

2.5 时间验证

#region IsDate(是否日期) /// <summary> /// 是否日期 /// </summary> /// <param>日期字符串</param> /// <param>是否正则验证</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>日期字符串</param> /// <param>格式化字符串</param> /// <returns></returns> public static bool IsDate(string value, string format) { return IsDate(value, format, null, DateTimeStyles.None); } /// <summary> /// 是否日期 /// </summary> /// <param>日期字符串</param> /// <param>格式化字符串</param> /// <param>格式化提供者</param> /// <param>日期格式</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>时间</param> /// <param>最小时间</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>时间</param> /// <param>最大时间</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验证

#region IsUrl(是否Url地址) /// <summary> /// 是否Url地址(统一资源定位) /// </summary> /// <param>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>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>url地址</param> /// <returns></returns> public static bool IsMainDomain(string value) { if (value.IsEmpty()) { return false; } return value.IsMatch( @"^http(s)?\://(()?[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 基础格式验证

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjsdyg.html