using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
namespace WMS.Util
{
///
/// 验证
///
public class ValidUtil
{
#region IsEmail(是否邮箱)
///
/// 是否邮箱
///
/// 邮箱地址
/// 是否按严格模式验证
///
public static bool IsEmail(string value, bool isRestrict = false)
{
if (string.IsNullOrWhiteSpace(value))
{
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 Regex.IsMatch(value, pattern, RegexOptions.IgnoreCase);
}
///
/// 是否存在邮箱
///
/// 值
/// 是否按严格模式验证
///
public static bool HasEmail(string value, bool isRestrict = false)
{
if (string.IsNullOrWhiteSpace(value))
{
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 Regex.IsMatch(value, pattern, RegexOptions.IgnoreCase);
}
#endregion
#region IsPhoneNumber(是否合法的手机号码)
///
/// 是否合法的手机号码
///
/// 手机号码
///
public static bool IsPhoneNumber(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
return Regex.IsMatch(value, @"^(0|86|17951)?(13[0-9]|15[012356789]|18[0-9]|14[57]|17[678])[0-9]{8}$");
}
#endregion
#region IsMobileNumber(是否手机号码)
///
/// 是否手机号码
///
/// 手机号码
/// 是否按严格模式验证
///
public static bool IsMobileNumberSimple(string value, bool isRestrict = false)
{
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
string pattern = isRestrict ? @"^[1][3-8]\d{9}$" : @"^[1]\d{10}$";
return Regex.IsMatch(value, pattern);
}
///
/// 是否手机号码
///
/// 手机号码
///
public static bool IsMobileNumber(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
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 Regex.IsMatch(value, @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|70)\d{8}$");
}
///
/// 是否存在手机号码
///
/// 值
/// 是否按严格模式验证
///
public static bool HasMobileNumberSimple(string value, bool isRestrict = false)
{
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
string pattern = isRestrict ? @"[1][3-8]\d{9}" : @"[1]\d{10}";
return Regex.IsMatch(value, pattern);
}
#endregion
#region IsChinaMobilePhone(是否中国移动号码)
///
/// 是否中国移动号码
///
/// 手机号码
///
public static bool IsChinaMobilePhone(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
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 Regex.IsMatch(value, @"(^1(3[4-9]|4[7]|5[0-27-9]|7[8]|8[2-478])\d{8}$)|(^1705\d{7}$)");
}
#endregion
#region IsChinaUnicomPhone(是否中国联通号码)
///
/// 是否中国联通号码
///
/// 手机号码
///
public static bool IsChinaUnicomPhone(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
/**
* 中国联通:China Unicom
* 130,131,132,155,156,185,186,145,176,1709
*/
return Regex.IsMatch(value, @"(^1(3[0-2]|4[5]|5[56]|7[6]|8[56])\d{8}$)|(^1709\d{7}$)");
}
#endregion
#region IsChinaTelecomPhone(是否中国电信号码)
///
/// 是否中国电信号码
///
/// 手机号码
///
public static bool IsChinaTelecomPhone(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
/**
* 中国电信:China Telecom
* 133,153,180,181,189,177,1700
*/
return Regex.IsMatch(value, @"(^1(33|53|77|8[019])\d{8}$)|(^1700\d{7}$)");
}
#endregion
#region IsIdCard(是否身份证号码)
///
/// 是否身份证号码
///
/// 身份证
///
public static bool IsIdCard(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
if (value.Length == 15)
{
return Regex.IsMatch(value, @"^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$");
}
return value.Length == 0x12 &&
Regex.IsMatch(value, @"^[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);
}
#endregion
#region IsBase64String(是否Base64编码)
///
/// 是否Base64编码
///
/// Base64字符串
///
public static bool IsBase64String(string value)
{
return Regex.IsMatch(value, @"[A-Za-z0-9\+\/\=]");
}
#endregion
#region IsDate(是否日期)
///
/// 是否日期
///
/// 日期字符串
/// 是否正则验证
///
public static bool IsDate(string value, bool isRegex = false)
{
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
if (isRegex)
{
//考虑到4年一度的366天,还有特殊的2月的日期
return
Regex.IsMatch(value,
@"^((((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);
}
///
/// 是否日期
///
/// 日期字符串
/// 格式化字符串
///
public static bool IsDate(string value, string format)
{
return IsDate(value, format, null, DateTimeStyles.None);
}
///
/// 是否日期
///
/// 日期字符串
/// 格式化字符串
/// 格式化提供者
/// 日期格式
///
public static bool IsDate(string value, string format, IFormatProvider provider, DateTimeStyles styles)
{
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
DateTime minValue;
return DateTime.TryParseExact(value, format, provider, styles, out minValue);
}
#endregion
#region IsDateTime(是否有效时间)
///
/// 是否大于最小时间
///
/// 时间
/// 最小时间
///
public static bool IsDateTimeMin(string value, DateTime min)
{
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
DateTime dateTime;
if (DateTime.TryParse(value, out dateTime))
{
if (DateTime.Compare(dateTime, min) >= 0)
{
return true;
}
}
return false;
}
///
/// 是否小于最大时间
///
/// 时间
/// 最大时间
///
public static bool IsDateTimeMax(string value, DateTime max)
{
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
DateTime dateTime;
if (DateTime.TryParse(value, out dateTime))
{
if (DateTime.Compare(max, dateTime) >= 0)
{
return true;
}
}
return false;
}
#endregion
#region IsGuid(是否Guid)
///
/// 是否Guid
///
/// Guid字符串
///
public static bool IsGuid(string guid)
{
if (string.IsNullOrWhiteSpace(guid))
{
return false;
}
return Regex.IsMatch(guid, @"[A-F0-9]{8}(-[A-F0-9]{4}){3}-[A-F0-9]{12}|[A-F0-9]{32}", RegexOptions.IgnoreCase);
}
#endregion
#region IsUrl(是否Url地址)
///
/// 是否Url地址(统一资源定位)
///
/// url地址
///
public static bool IsUrl(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
return
Regex.IsMatch(value,
@"^(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)
///
/// 是否Uri(统一资源标识)
///
/// uri
///
public static bool IsUri(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
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 IsMac(是否Mac地址)
///
/// 是否Mac地址
///
/// Mac地址
///
public static bool IsMac(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
return Regex.IsMatch(value, @"^([0-9A-F]{2}-){5}[0-9A-F]{2}$") || Regex.IsMatch(value, @"^[0-9A-F]{12}$");
}
#endregion
#region IsPositiveInteger(是否大于0的正整数)
///
/// 是否大于0的正整数
///
/// 正整数
///
public static bool IsPositiveInteger(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
return Regex.IsMatch(value, @"^[1-9]+\d*$");
}
#endregion
#region IsInt32(是否Int32类型)
///
/// 是否Int32类型
///
/// 整数
///
public static bool IsInt32(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
return Regex.IsMatch(value, @"^[0-9]*$");
}
#endregion
#region IsDouble(是否Double类型,如果带有.默认为1位0)
///
/// 是否Double类型
///
/// 小数
///
public static bool IsDouble(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
return Regex.IsMatch(value, @"^\d[.]?\d?$");
}
///
/// 是否Double类型
///
/// 小数
/// 最小值
/// 最大值
/// 小数位数,如果是0则不检测
///
public static bool IsDouble(string value, double minValue, double maxValue, int digit)
{
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
string patten = string.Format(@"^\d[.]?\d{0}$", "{0,10}");
if (digit > 0)
{
patten = string.Format(@"^\d[.]?\d{0}$", "{" + digit + "}");
}
if (Regex.IsMatch(value, patten))
{
double val = Convert.ToDouble(value);
if (val >= minValue && val <= maxValue)
{
return true;
}
}
return false;
}
#endregion
#region IsPassword(是否密码格式)
///
/// 是否密码One的格式,6-25位包含特殊字符
///
/// 密码
///
public static bool IsPasswordOne(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
return Regex.IsMatch(value, @"^[\@A-Za-z0-9\!\#\$\%\^\&\*\.\~_]{6,25}$");
}
///
/// 是否密码One的格式,指定密码长度,包含特殊字符
///
/// 密码
/// 最小长度
/// 最大长度
///
public static bool IsPasswordOne(string value, int min, int max)
{
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
return Regex.IsMatch(value, string.Format(@"^[\@A-Za-z0-9\!\#\$\%\^\&\*\.\~_]{0},{1}$", "{" + min, max + "}"));
}
///
/// 是否密码Two的格式,6-25位包含特殊字符
///
/// 密码
///
public static bool IsPasswordTwo(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
return
Regex.IsMatch(value,
@"(?=^.{6,25}$)(?=(?:.*?\d){1})(?=.*[a-z])(?=(?:.*?[A-Z]){1})(?=(?:.*?[!@#$%*()_+^&}{:;?.]){1})(?!.*\s)[0-9a-zA-Z!@#$%*()_+^&]*$");
}
#endregion
#region IsLoginName(是否登录账号)
///
/// 是否登录账号,6-30位长度
///
/// 登录账号
///
public static bool IsLoginName(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
return Regex.IsMatch(value, @"^(?![^a-zA-Z]+$)[A-Za-z0-9]{6,30}$");
}
///
/// 是否登录账号
///
/// 登录账号
/// 最小长度
/// 最大长度
///
public static bool IsLoginName(string value, int min, int max)
{
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
return Regex.IsMatch(value, string.Format(@"^(?![^a-zA-Z]+$)[A-Za-z0-9]{0},{1}$", "{" + min, max + "}"));
}
#endregion
#region IsBankCard(是否银行卡号)
///
/// 是否银行卡号,16位或19位银行卡(简单校验)
///
/// 银行卡号
///
public static bool IsBandCard(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
return Regex.IsMatch(value, @"^(\d{16}|\d{19})$");
}
#endregion
#region IsSafeSqlString(是否安全Sql语句)
///
/// 是否安全Sql语句
///
/// sql语句
///
public static bool IsSafeSqlString(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
return !Regex.IsMatch(value, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']");
}
#endregion
#region IsVersion(是否有效的版本号)
///
/// 是否有效版本号,范例:1.3,1.1.5,1.25.256
///
/// 版本号
/// 长度
///
public static bool IsVersion(string value, int length = 5)
{
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
value = value.Replace("^", "").Replace("$", "");
return Regex.IsMatch(value, string.Format(@"^{0}{1}{2}$", @"\d{0,4}\.(\d{1,4}\.){0,", length, @"}\d{1,4}"));
}
#endregion
#region IsContainsChinese(是否包含中文)
///
/// 是否中文
///
/// 中文
///
public static bool IsChinese(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
return Regex.IsMatch(value, @"^[\u4e00-\u9fa5]+$", RegexOptions.IgnoreCase);
}
///
/// 是否包含中文
///
/// 中文
///
public static bool IsContainsChinese(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
return Regex.IsMatch(value, @"[\u4e00-\u9fa5]+", RegexOptions.IgnoreCase);
}
#endregion
#region IsContainsNumber(是否包含数字)
///
/// 是否包含数字
///
/// 数字
///
public static bool IsContainsNumber(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
return Regex.IsMatch(value, @"[0-9]+");
}
#endregion
#region IsMainDomain(是否主域名)
///
/// 是否主域名或者www开头的域名
///
/// url地址
///
public static bool IsMainDomain(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
return
Regex.IsMatch(value,
@"^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
#region IsMobileUser(是否手机用户)
///
/// 是否手机用户
///
/// 上下文
///
//public static bool IsMobileUser(HttpContext context = null)
//{
// if (context == null)
// {
// context = HttpContext.Current;
// }
// if (context != null)
// {
// if (context.Request.Browser.IsMobileDevice)
// {
// return true;
// }
// if (!string.IsNullOrWhiteSpace(context.Request.UserAgent))
// {
// return
// Regex.IsMatch(context.Request.UserAgent,
// @"(iemobile|iphone|ipod|android|nokia|sonyericsson|blackberry|samsung|sec\-|windows ce|motorola|mot\-|up.b|midp\-)",
// RegexOptions.IgnoreCase | RegexOptions.Compiled);
// }
// }
// return false;
//}
#endregion
#region IsIpAddress(是否IP地址)
///
/// 是否IP地址
///
/// ip地址
/// 结果
public static bool IsIpAddress(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
return Regex.IsMatch(value, @"^(\d(25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])\d\.){3}\d(25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])\d$");
}
#endregion
#region IsInteger(是否整数)
///
/// 是否整数
///
/// 值
/// 结果
public static bool IsInteger(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
return Regex.IsMatch(value, @"^\-?[0-9]+$");
}
#endregion
#region IsUnicode(是否Unicode字符串)
///
/// 是否Unicode字符串
///
/// unicode字符串
/// 结果
public static bool IsUnicode(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
return
Regex.IsMatch(value,
@"^(http|https|ftp|rtsp|mms):(\/\/|\\\\)[A-Za-z0-9%\-_@]+\.[A-Za-z0-9%\-_@]+[A-Za-z0-9\.\/=\?%\-&_~`@:\+!;]*$");
}
#endregion
#region IsLengthStr(字符串长度是否在指定范围内)
///
/// 字符串长度是否在指定范围内,一个中文为2个字符
///
/// 字符串
/// 开始
/// 结束
///
public static bool IsLengthStr(string value, int begin, int end)
{
int length = Regex.Replace(value, @"[^\x00-\xff]", "OK").Length;
if ((length <= begin) && (length >= end))
{
return false;
}
return true;
}
#endregion
#region IsTel(是否中国电话)
///
/// 是否中国电话,格式:010-85849685
///
/// 电话
///
public static bool IsTel(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
return Regex.IsMatch(value, @"^\d{3,4}-?\d{6,8}$", RegexOptions.IgnoreCase);
}
#endregion
#region IsPostalCode(是否邮政编码)
///
/// 是否邮政编码,6位数字
///
/// 邮政编码
///
public static bool IsPostalCode(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
return Regex.IsMatch(value, @"^[1-9]\d{5}$", RegexOptions.IgnoreCase);
}
#endregion
#region IsNormalChar(是否正常字符,字母、数字、下划线的组合)
///
/// 是否正常字符,字母、数字、下划线的组合
///
/// 字符串
///
public static bool IsNormalChar(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
return Regex.IsMatch(value, @"[\w\d_]+", RegexOptions.IgnoreCase);
}
#endregion
#region IsPostfix(是否指定后缀)
///
/// 是否指定后缀
///
/// 字符串
/// 后缀名数组
///
public static bool IsPostfix(string value, string[] postfixs)
{
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
string postfix = string.Join("|", postfixs);
return Regex.IsMatch(value, string.Format(@".(?i:{0})$", postfix));
}
#endregion
#region IsDecimal(是否数字型)
///
/// 是否数字型
///
/// 数字
///
public static bool IsDecimal(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
return Regex.IsMatch(value, @"^([0-9])[0-9]*(\.\w*)?$");
}
#endregion
#region IsBase64(是否Base64字符串)
///
/// 是否Base64字符串
///
/// 字符串
///
public static bool IsBase64(string value)
{
if (value.Length % 4 != 0)
{
return false;
}
return Regex.IsMatch(value, @"^[A-Z0-9/+=]*$", RegexOptions.IgnoreCase);
}
#endregion
#region IsRepeat(是否重复)
///
/// 是否重复,范例:112,返回true
///
/// 值
///
public static bool IsRepeat(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
var array = value.ToCharArray();
return array.Any(c => array.Count(t => t == c) > 1);
}
#endregion
#region IsQQ(是否合法QQ号码)
///
/// 是否合法QQ号码
///
/// QQ号码
///
// ReSharper disable once InconsistentNaming
public static bool IsQQ(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
return Regex.IsMatch(value, @"^[1-9][0-9]{4,9}$");
}
#endregion
}
}