/*一些常用的正则表达式
*
*
*
^\d+$ //匹配非负整数(正整数 + 0)
^[0-9]*[1-9][0-9]*$ //匹配正整数
^((-\d+)|(0+))$ //匹配非正整数(负整数 + 0)
^-[0-9]*[1-9][0-9]*$ //匹配负整数
^-?\d+$ //匹配整数
^\d+(\.\d+)?$ //匹配非负浮点数(正浮点数 + 0)
^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$ //匹配正浮点数
^((-\d+(\.\d+)?)|(0+(\.0+)?))$ //匹配非正浮点数(负浮点数 + 0)
^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$ //匹配负浮点数
^(-?\d+)(\.\d+)?$ //匹配浮点数
^[A-Za-z]+$ //匹配由26个英文字母组成的字符串
^[A-Z]+$ //匹配由26个英文字母的大写组成的字符串
^[a-z]+$ //匹配由26个英文字母的小写组成的字符串
^[A-Za-z0-9]+$ //匹配由数字和26个英文字母组成的字符串
^\w+$ //匹配由数字、26个英文字母或者下划线组成的字符串
^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$ //匹配email地址
^[a-zA-z]+://匹配(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$ //匹配url
匹配中文字符的正则表达式: [\u4e00-\u9fa5]
匹配双字节字符(包括汉字在内):[^\x00-\xff]
匹配空行的正则表达式:\n[\s| ]*\r
匹配HTML标记的正则表达式:/<(.*)>.*<\/>|<(.*) \/>/
匹配首尾空格的正则表达式:(^\s*)|(\s*$)
匹配Email地址的正则表达式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
匹配网址URL的正则表达式:^[a-zA-z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$
匹配帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线):^[a-zA-Z][a-zA-Z0-9_]{4,15}$
匹配国内电话号码:(\d{3}-|\d{4}-)?(\d{8}|\d{7})?
匹配腾讯QQ号:^[1-9]*[1-9][0-9]*$
* */
private static Regex RegNumber = new Regex("^[0-9]+$");
private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+$");
private static Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$");
private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); //等价于^[+-]?\d+[.]?\d+$
private static Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");//w 英文字母或数字的字符串,和 [a-zA-Z0-9] 语法一样
private static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]");
protected DataValidate()
{
}
public static bool IsAreaCode(string input)
{
return ((IsNumber(input) && (input.Length >= 3)) && (input.Length <= 5));
}
public static bool IsDecimal(string input)
{
if (string.IsNullOrEmpty(input))
{
return false;
}
return Regex.IsMatch(input, "^[0-9]+[.]?[0-9]+$");
}
public static bool IsDecimalSign(string input)
{
if (string.IsNullOrEmpty(input))
{
return false;
}
return Regex.IsMatch(input, "^[+-]?[0-9]+[.]?[0-9]+$");
}
/// <summary>
/// 检测是否符合email格式
/// </summary>
/// <param name="strEmail">要判断的email字符串</param>
/// <returns>判断结果</returns>
public static bool IsEmail(string strEmail)
{
if (string.IsNullOrEmpty(strEmail))
{
return false;
}
return Regex.IsMatch(strEmail, @"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
}
public static bool IsIP(string strIp)
{
return (!string.IsNullOrEmpty(strIp) && Regex.IsMatch(strIp.Trim(), @"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$"));
}
/// <summary>
/// 判断给定的字符串(strNumber)是否是数值型
/// </summary>
/// <param name="strNumber">要确认的字符串</param>
/// <returns>是则返加true 不是则返回 false</returns>
public static bool IsNumber(string strNumber)
{
return Regex.IsMatch(strNumber, "^[0-9]+$");
}
public static bool IsNumberSign(string input)
{
if (string.IsNullOrEmpty(input))
{
return false;
}
return Regex.IsMatch(input, "^[+-]?[0-9]+$");
}
/// <summary>
/// 检测是否符合邮编格式
/// </summary>
/// <param name="postCode"></param>
/// <returns></returns>
public static bool IsPostCode(string postCode)
{
return Regex.IsMatch(postCode, @"^\d{6}$");
}
/// <summary>
/// 检测是否符合身份证号码格式
/// </summary>
/// <param name="num"></param>
/// <returns></returns>
public static bool IsIdentityNumber(string num)
{
return Regex.IsMatch(num, @"^\d{17}[\d|X]|\d{15}$");
}
/// <summary>
/// 检测是否符合时间格式
/// </summary>
/// <returns></returns>
public static bool IsTime(string timeval)
{
return Regex.IsMatch(timeval, @"20\d{2}\-[0-1]{1,2}\-[0-3]?[0-9]?(\s*((([0-1]?[0-9])|(2[0-3])):([0-5]?[0-9])(:[0-5]?[0-9])?))?");
}
/// <summary>
/// 检测是否符合url格式,前面必需含有http://
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static bool IsURL(string url)
{
if (string.IsNullOrEmpty(url))
{
return false;
}
return Regex.IsMatch(url, @"^http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?$");
}
/// <summary>
/// 检测是否符合电话格式
/// </summary>
/// <param name="phoneNumber"></param>
/// <returns></returns>
public static bool IsPhoneNumber(string phoneNumber)
{
return Regex.IsMatch(phoneNumber, @"^(\(\d{3}\)|\d{3}-)?\d{7,8}$");
}
public static bool IsValidId(string input)
{
if (string.IsNullOrEmpty(input))
{
return false;
}
input = input.Replace("|", "").Replace(",", "").Replace("-", "").Replace(" ", "").Trim();
if (string.IsNullOrEmpty(input))
{
return false;
}
return IsNumber(input);
}
public static bool IsValidUserName(string userName, out string msg)
{
userName = userName.Trim();
msg = "";
if (string.IsNullOrEmpty(userName))
{
msg = "用户名不能为空";
return false;
}
if (StringHelper.GetStringLength( userName) > 20)
{
msg = "用户名长度不能大于32";
return false;
}
if (StringHelper.GetStringLength(userName) < 4)
{
msg = "用户名长度不能小于4";
return false;
}
string str = "\\/\"[]:|<>+=;,?*@.";
for (int i = 0; i < userName.Length; i++)
{
if (str.IndexOf(userName[i]) >= 0)
{
msg = "用户名含有非法字符";
return false;
}
}
return true;
}
/// <summary>
/// 验证密码长度是否小于6
/// </summary>
/// <param name="password"></param>
/// <returns></returns>
public static bool IsTooShortPassword(string password)
{
return StringHelper.GetStringLength(password.Trim()) >= 6 ? false : true;
}
#region 中文检测
/// <summary>
/// 检测是否有中文字符
/// </summary>
/// <param name="inputData"></param>
/// <returns></returns>
public static bool IsHasCHZN(string inputData)
{
Match m = RegCHZN.Match(inputData);
return m.Success;
}
#endregion
*
*
*
^\d+$ //匹配非负整数(正整数 + 0)
^[0-9]*[1-9][0-9]*$ //匹配正整数
^((-\d+)|(0+))$ //匹配非正整数(负整数 + 0)
^-[0-9]*[1-9][0-9]*$ //匹配负整数
^-?\d+$ //匹配整数
^\d+(\.\d+)?$ //匹配非负浮点数(正浮点数 + 0)
^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$ //匹配正浮点数
^((-\d+(\.\d+)?)|(0+(\.0+)?))$ //匹配非正浮点数(负浮点数 + 0)
^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$ //匹配负浮点数
^(-?\d+)(\.\d+)?$ //匹配浮点数
^[A-Za-z]+$ //匹配由26个英文字母组成的字符串
^[A-Z]+$ //匹配由26个英文字母的大写组成的字符串
^[a-z]+$ //匹配由26个英文字母的小写组成的字符串
^[A-Za-z0-9]+$ //匹配由数字和26个英文字母组成的字符串
^\w+$ //匹配由数字、26个英文字母或者下划线组成的字符串
^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$ //匹配email地址
^[a-zA-z]+://匹配(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$ //匹配url
匹配中文字符的正则表达式: [\u4e00-\u9fa5]
匹配双字节字符(包括汉字在内):[^\x00-\xff]
匹配空行的正则表达式:\n[\s| ]*\r
匹配HTML标记的正则表达式:/<(.*)>.*<\/>|<(.*) \/>/
匹配首尾空格的正则表达式:(^\s*)|(\s*$)
匹配Email地址的正则表达式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
匹配网址URL的正则表达式:^[a-zA-z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$
匹配帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线):^[a-zA-Z][a-zA-Z0-9_]{4,15}$
匹配国内电话号码:(\d{3}-|\d{4}-)?(\d{8}|\d{7})?
匹配腾讯QQ号:^[1-9]*[1-9][0-9]*$
* */
private static Regex RegNumber = new Regex("^[0-9]+$");
private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+$");
private static Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$");
private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); //等价于^[+-]?\d+[.]?\d+$
private static Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");//w 英文字母或数字的字符串,和 [a-zA-Z0-9] 语法一样
private static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]");
protected DataValidate()
{
}
public static bool IsAreaCode(string input)
{
return ((IsNumber(input) && (input.Length >= 3)) && (input.Length <= 5));
}
public static bool IsDecimal(string input)
{
if (string.IsNullOrEmpty(input))
{
return false;
}
return Regex.IsMatch(input, "^[0-9]+[.]?[0-9]+$");
}
public static bool IsDecimalSign(string input)
{
if (string.IsNullOrEmpty(input))
{
return false;
}
return Regex.IsMatch(input, "^[+-]?[0-9]+[.]?[0-9]+$");
}
/// <summary>
/// 检测是否符合email格式
/// </summary>
/// <param name="strEmail">要判断的email字符串</param>
/// <returns>判断结果</returns>
public static bool IsEmail(string strEmail)
{
if (string.IsNullOrEmpty(strEmail))
{
return false;
}
return Regex.IsMatch(strEmail, @"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
}
public static bool IsIP(string strIp)
{
return (!string.IsNullOrEmpty(strIp) && Regex.IsMatch(strIp.Trim(), @"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$"));
}
/// <summary>
/// 判断给定的字符串(strNumber)是否是数值型
/// </summary>
/// <param name="strNumber">要确认的字符串</param>
/// <returns>是则返加true 不是则返回 false</returns>
public static bool IsNumber(string strNumber)
{
return Regex.IsMatch(strNumber, "^[0-9]+$");
}
public static bool IsNumberSign(string input)
{
if (string.IsNullOrEmpty(input))
{
return false;
}
return Regex.IsMatch(input, "^[+-]?[0-9]+$");
}
/// <summary>
/// 检测是否符合邮编格式
/// </summary>
/// <param name="postCode"></param>
/// <returns></returns>
public static bool IsPostCode(string postCode)
{
return Regex.IsMatch(postCode, @"^\d{6}$");
}
/// <summary>
/// 检测是否符合身份证号码格式
/// </summary>
/// <param name="num"></param>
/// <returns></returns>
public static bool IsIdentityNumber(string num)
{
return Regex.IsMatch(num, @"^\d{17}[\d|X]|\d{15}$");
}
/// <summary>
/// 检测是否符合时间格式
/// </summary>
/// <returns></returns>
public static bool IsTime(string timeval)
{
return Regex.IsMatch(timeval, @"20\d{2}\-[0-1]{1,2}\-[0-3]?[0-9]?(\s*((([0-1]?[0-9])|(2[0-3])):([0-5]?[0-9])(:[0-5]?[0-9])?))?");
}
/// <summary>
/// 检测是否符合url格式,前面必需含有http://
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static bool IsURL(string url)
{
if (string.IsNullOrEmpty(url))
{
return false;
}
return Regex.IsMatch(url, @"^http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?$");
}
/// <summary>
/// 检测是否符合电话格式
/// </summary>
/// <param name="phoneNumber"></param>
/// <returns></returns>
public static bool IsPhoneNumber(string phoneNumber)
{
return Regex.IsMatch(phoneNumber, @"^(\(\d{3}\)|\d{3}-)?\d{7,8}$");
}
public static bool IsValidId(string input)
{
if (string.IsNullOrEmpty(input))
{
return false;
}
input = input.Replace("|", "").Replace(",", "").Replace("-", "").Replace(" ", "").Trim();
if (string.IsNullOrEmpty(input))
{
return false;
}
return IsNumber(input);
}
public static bool IsValidUserName(string userName, out string msg)
{
userName = userName.Trim();
msg = "";
if (string.IsNullOrEmpty(userName))
{
msg = "用户名不能为空";
return false;
}
if (StringHelper.GetStringLength( userName) > 20)
{
msg = "用户名长度不能大于32";
return false;
}
if (StringHelper.GetStringLength(userName) < 4)
{
msg = "用户名长度不能小于4";
return false;
}
string str = "\\/\"[]:|<>+=;,?*@.";
for (int i = 0; i < userName.Length; i++)
{
if (str.IndexOf(userName[i]) >= 0)
{
msg = "用户名含有非法字符";
return false;
}
}
return true;
}
/// <summary>
/// 验证密码长度是否小于6
/// </summary>
/// <param name="password"></param>
/// <returns></returns>
public static bool IsTooShortPassword(string password)
{
return StringHelper.GetStringLength(password.Trim()) >= 6 ? false : true;
}
#region 中文检测
/// <summary>
/// 检测是否有中文字符
/// </summary>
/// <param name="inputData"></param>
/// <returns></returns>
public static bool IsHasCHZN(string inputData)
{
Match m = RegCHZN.Match(inputData);
return m.Success;
}
#endregion