asp.net常用正则表达式
正则表达式在我们判断字符的时候是用的最多的了,为了以后的方便,摘抄几个,备忘啊,呵呵
using System;
using System.Text.RegularExpressions;
using System.Configuration;
public class ValidateBussiness
{
/// <summary>
/// 判断:输入框不能为空,并且输入值为email,返回值为bool型
/// </summary>
public bool Email(String ValidateString)
{
ValidateString = ValidateString.Trim().ToLower();
if (ValidateString.Trim().Length == 0)
{
return false;
}
else
{
Match m = Regex.Match(ValidateString, @"^([\-_\.a-z0-9]+@[\-_a-z0-9]+[\.][a-z0-9]{2,})+$");
if (m.Success)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// 判断:输入框可为空,不为空时值为email,返回值为bool型
/// </summary>
public bool E_Email(String ValidateString)
{
ValidateString = ValidateString.Trim();
if (ValidateString.Trim().Length == 0)
{
return true;
}
else
{
Match m = Regex.Match(ValidateString, @"^([_\.a-z0-9]+@[a-z0-9]+[\.][a-z0-9]{2,})+$");
if (m.Success)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// 判断输入框不能为空,并且输入值为中文
/// </summary>
//
public bool ChinaString(String ValidateString)
{
ValidateString = ValidateString.Trim();
if (ValidateString.Length == 0)
{
return false;
}
else
{
Match m = Regex.Match(ValidateString, @"^[\u4e00-\u9fa5]+$");
if (m.Success)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// 英文和中文,输入不能为空
/// </summary>
/// <param name="ValidateString"></param>
/// <returns></returns>
public bool ECString(String ValidateString)
{
ValidateString = ValidateString.Trim();
if (ValidateString.Length == 0)
{
return false;
}
else
{
Match m = Regex.Match(ValidateString, @"^[A-Za-z\u4e00-\u9fa5]+$");
if (m.Success)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// 不为空的字符串,只可以包含英文中文和数字
/// </summary>
/// <param name="ValidateString"></param>
/// <returns></returns>
public bool NotEString(String ValidateString)
{
ValidateString = ValidateString.Trim();
if (ValidateString.Length == 0)
{
return false;
}
else
{
Match m = Regex.Match(ValidateString, @"^[A-Za-z0-9\u4e00-\u9fa5]+$");
if (m.Success)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// 不为空的字符串,可以包含特殊字符,但不能包含引号和空格等
/// </summary>
/// <param name="ValidateString"></param>
/// <returns></returns>
public bool NotEString2(String ValidateString)
{
ValidateString = ValidateString.Trim();
if (ValidateString.Length == 0)
{
return false;
}
else
{
Match m = Regex.Match(ValidateString, @"^[A-Za-z0-9\u4e00-\u9fa5-\~\!\@\#\$\%\^\&\*\(\)\+\-\=\{\}\[\]\|\<\>\?._,]+$");
if (m.Success)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// 判断输入框可为空,不为空时值为中文
/// </summary>
//
public bool E_ChinaString(String ValidateString)
{
ValidateString = ValidateString.Trim();
if (ValidateString.Length == 0)
{
return true;
}
else
{
Match m = Regex.Match(ValidateString, @"^[\u4e00-\u9fa5]+$");
if (m.Success)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// 输入框不可为空,不为空时值为浮点数
/// </summary>
//
public bool Float(String ValidateString)
{
ValidateString = ValidateString.Trim();
if (ValidateString.Length == 0)
{
return false;
}
else
{
Match m = Regex.Match(ValidateString, @"^(-?\d+)(\.\d+)?$");
if (m.Success)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// 判断输入框可为空,不为空时值为浮点数
/// </summary>
//
public bool E_Float(String ValidateString)
{
ValidateString = ValidateString.Trim();
if (ValidateString.Length == 0)
{
return true;
}
else
{
Match m = Regex.Match(ValidateString, @"^(-?\d+)(\.\d+)?$");
if (m.Success)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// 判断:输入框不能为空,并且输入值为数字
/// </summary>
//
public bool Number(String ValidateString)
{
ValidateString = ValidateString.Trim();
if (ValidateString.Length == 0)
{
return false;
}
else
{
Match m = Regex.Match(ValidateString, @"^[0-9]+$");
if (m.Success)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// 输入框可为空,不为空时值为数字
/// </summary>
//
public bool E_Number(String ValidateString)
{
ValidateString = ValidateString.Trim();
if (ValidateString.Length == 0)
{
return true;
}
else
{
Match m = Regex.Match(ValidateString, @"^[0-9]+$");
if (m.Success)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
///判断输入框不能为空,并且输入值为有效日期
/// </summary>
public bool Date(String ValidateString)
{
ValidateString = ValidateString.Trim();
if (ValidateString.Length == 0)
{
return false;
}
else
{
Match m = Regex.Match(ValidateString, @"^([0-9]{4}-(0[1-9]|[1-9]|1[0-2])-((0[1-9]|[1-9])|1[0-9]|2[0-9]|3[0-1]))+$");
if (m.Success)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// 判断2000年以后的年份
/// </summary>
/// <param name="ValidateString"></param>
/// <returns></returns>
public bool DateYear(String ValidateString)
{
ValidateString = ValidateString.Trim();
if (ValidateString.Length == 0)
{
return false;
}
else
{
Match m = Regex.Match(ValidateString, @"^(\b2[0-9]{3}\b)");
if (m.Success)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
///判断输入框可为空,不为空时值为日期
/// </summary>
//
public bool E_Date(String ValidateString)
{
ValidateString = ValidateString.Trim();
if (ValidateString.Length == 0)
{
return true;
}
else
{
Match m = Regex.Match(ValidateString, @"^([0-9]{4}-(0[1-9]|[1-9]|1[0-2])-((0[1-9]|[1-9])|1[0-9]|2[0-9]|3[0-1]))+$");
if (m.Success)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
///判断输入框不能为空,并且输入值为<=100正浮点数(百分比)
/// </summary>
//
public bool Percent(String ValidateString)
{
ValidateString = ValidateString.Trim();
if (ValidateString.Length == 0)
{
return false;
}
else
{
Match m = Regex.Match(ValidateString, @"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$");
if (m.Success)
{
if (Convert.ToDouble(ValidateString) <= 100)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
}
/// <summary>
///判断输入框可为空,不为空时输入值为<=100正浮点数(百分比)
/// </summary>
//
public bool E_Percent(String ValidateString)
{
ValidateString = ValidateString.Trim();
if (ValidateString.Length == 0)
{
return true;
}
else
{
Match m = Regex.Match(ValidateString, @"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$");
if (m.Success)
{
if (Convert.ToDouble(ValidateString) <= 100)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
}
/// <summary>
/////判断输入框不能为空,并且输入值为6位数字的邮政编码
/// </summary>
public bool PostalCode(String ValidateString)
{
ValidateString = ValidateString.Trim();
if (ValidateString.Length != 6)
{
return false;
}
else
{
Match m = Regex.Match(ValidateString, @"^[0-9]+$");
if (m.Success)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/////判断输入框不能为空,并且输入值为6位数字的邮政编码
/// </summary>
public bool E_PostalCode(String ValidateString)
{
ValidateString = ValidateString.Trim();
if (ValidateString.Length == 0)
{
return true;
}
else
{
Match m = Regex.Match(ValidateString, @"^[0-9]+$");
if (m.Success)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/////处理数据,在把数据存到数据库前,先屏蔽那些危险字符!
/// </summary>
public string DealString(string str)
{
str = str + "1";
string str1 = str.ToLower();
string[] vv ={ "count", "*", "%", "chr", "mid", "master", "'", "<", ">", "\r", "\'", "\x0020", "@", ";", "and", "(", ")", "exec", "insert", "select", "delete", "update", "truncate", "char", "declare" };
for (int i = 0; i < vv.Length; i++)
{
while (str1.IndexOf(vv[i], 0, str1.Length - 1) != -1)
{
int bb = str1.IndexOf(vv[i], 0, str1.Length - 1);
str1 = str1.Remove(bb, vv[i].Length);
str = str.Remove(bb, vv[i].Length);
}
}
str = str.Remove(str.Length - 1, 1);
return (str);
}
/// <summary>
/////恢复数据:把数据库中的数据,还原成未处理前的样子!
/// </summary>
//
public string UnDealString(string str)
{
str = str.Replace("<", "<");
str = str.Replace(">", ">");
str = str.Replace("<br>", "\r");
str = str.Replace("’", "\'");
str = str.Replace(" ", "\x0020");
str = str.Replace("''", "'");
return (str);
}
/// <summary>
/// 判断:输入框不能为空,并且输入值为大雨零的整数
/// </summary>
//
public bool NumberExceptZero(String ValidateString)
{
ValidateString = ValidateString.Trim();
if (ValidateString.Length == 0)
{
return false;
}
else
{
Match m = Regex.Match(ValidateString, @"^[1-9]+$");
if (m.Success)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// 大于0小于50
/// </summary>
/// <param name="ValidateString"></param>
/// <returns></returns>
public bool StringLessThan50(string ValidateString)
{
if (ValidateString.Length <= 50 && ValidateString.Length >0)
{
return true ;
}
else
return false ;
}
/// <summary>
/// 大于0小于500
/// </summary>
/// <param name="ValidateString"></param>
/// <returns></returns>
public bool StringLessThan500(string ValidateString)
{
if (ValidateString.Length <=500 && ValidateString .Length >0)
{
return true ;
}
else
return false ;
}
}
using System.Text.RegularExpressions;
using System.Configuration;
public class ValidateBussiness
{
/// <summary>
/// 判断:输入框不能为空,并且输入值为email,返回值为bool型
/// </summary>
public bool Email(String ValidateString)
{
ValidateString = ValidateString.Trim().ToLower();
if (ValidateString.Trim().Length == 0)
{
return false;
}
else
{
Match m = Regex.Match(ValidateString, @"^([\-_\.a-z0-9]+@[\-_a-z0-9]+[\.][a-z0-9]{2,})+$");
if (m.Success)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// 判断:输入框可为空,不为空时值为email,返回值为bool型
/// </summary>
public bool E_Email(String ValidateString)
{
ValidateString = ValidateString.Trim();
if (ValidateString.Trim().Length == 0)
{
return true;
}
else
{
Match m = Regex.Match(ValidateString, @"^([_\.a-z0-9]+@[a-z0-9]+[\.][a-z0-9]{2,})+$");
if (m.Success)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// 判断输入框不能为空,并且输入值为中文
/// </summary>
//
public bool ChinaString(String ValidateString)
{
ValidateString = ValidateString.Trim();
if (ValidateString.Length == 0)
{
return false;
}
else
{
Match m = Regex.Match(ValidateString, @"^[\u4e00-\u9fa5]+$");
if (m.Success)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// 英文和中文,输入不能为空
/// </summary>
/// <param name="ValidateString"></param>
/// <returns></returns>
public bool ECString(String ValidateString)
{
ValidateString = ValidateString.Trim();
if (ValidateString.Length == 0)
{
return false;
}
else
{
Match m = Regex.Match(ValidateString, @"^[A-Za-z\u4e00-\u9fa5]+$");
if (m.Success)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// 不为空的字符串,只可以包含英文中文和数字
/// </summary>
/// <param name="ValidateString"></param>
/// <returns></returns>
public bool NotEString(String ValidateString)
{
ValidateString = ValidateString.Trim();
if (ValidateString.Length == 0)
{
return false;
}
else
{
Match m = Regex.Match(ValidateString, @"^[A-Za-z0-9\u4e00-\u9fa5]+$");
if (m.Success)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// 不为空的字符串,可以包含特殊字符,但不能包含引号和空格等
/// </summary>
/// <param name="ValidateString"></param>
/// <returns></returns>
public bool NotEString2(String ValidateString)
{
ValidateString = ValidateString.Trim();
if (ValidateString.Length == 0)
{
return false;
}
else
{
Match m = Regex.Match(ValidateString, @"^[A-Za-z0-9\u4e00-\u9fa5-\~\!\@\#\$\%\^\&\*\(\)\+\-\=\{\}\[\]\|\<\>\?._,]+$");
if (m.Success)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// 判断输入框可为空,不为空时值为中文
/// </summary>
//
public bool E_ChinaString(String ValidateString)
{
ValidateString = ValidateString.Trim();
if (ValidateString.Length == 0)
{
return true;
}
else
{
Match m = Regex.Match(ValidateString, @"^[\u4e00-\u9fa5]+$");
if (m.Success)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// 输入框不可为空,不为空时值为浮点数
/// </summary>
//
public bool Float(String ValidateString)
{
ValidateString = ValidateString.Trim();
if (ValidateString.Length == 0)
{
return false;
}
else
{
Match m = Regex.Match(ValidateString, @"^(-?\d+)(\.\d+)?$");
if (m.Success)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// 判断输入框可为空,不为空时值为浮点数
/// </summary>
//
public bool E_Float(String ValidateString)
{
ValidateString = ValidateString.Trim();
if (ValidateString.Length == 0)
{
return true;
}
else
{
Match m = Regex.Match(ValidateString, @"^(-?\d+)(\.\d+)?$");
if (m.Success)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// 判断:输入框不能为空,并且输入值为数字
/// </summary>
//
public bool Number(String ValidateString)
{
ValidateString = ValidateString.Trim();
if (ValidateString.Length == 0)
{
return false;
}
else
{
Match m = Regex.Match(ValidateString, @"^[0-9]+$");
if (m.Success)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// 输入框可为空,不为空时值为数字
/// </summary>
//
public bool E_Number(String ValidateString)
{
ValidateString = ValidateString.Trim();
if (ValidateString.Length == 0)
{
return true;
}
else
{
Match m = Regex.Match(ValidateString, @"^[0-9]+$");
if (m.Success)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
///判断输入框不能为空,并且输入值为有效日期
/// </summary>
public bool Date(String ValidateString)
{
ValidateString = ValidateString.Trim();
if (ValidateString.Length == 0)
{
return false;
}
else
{
Match m = Regex.Match(ValidateString, @"^([0-9]{4}-(0[1-9]|[1-9]|1[0-2])-((0[1-9]|[1-9])|1[0-9]|2[0-9]|3[0-1]))+$");
if (m.Success)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// 判断2000年以后的年份
/// </summary>
/// <param name="ValidateString"></param>
/// <returns></returns>
public bool DateYear(String ValidateString)
{
ValidateString = ValidateString.Trim();
if (ValidateString.Length == 0)
{
return false;
}
else
{
Match m = Regex.Match(ValidateString, @"^(\b2[0-9]{3}\b)");
if (m.Success)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
///判断输入框可为空,不为空时值为日期
/// </summary>
//
public bool E_Date(String ValidateString)
{
ValidateString = ValidateString.Trim();
if (ValidateString.Length == 0)
{
return true;
}
else
{
Match m = Regex.Match(ValidateString, @"^([0-9]{4}-(0[1-9]|[1-9]|1[0-2])-((0[1-9]|[1-9])|1[0-9]|2[0-9]|3[0-1]))+$");
if (m.Success)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
///判断输入框不能为空,并且输入值为<=100正浮点数(百分比)
/// </summary>
//
public bool Percent(String ValidateString)
{
ValidateString = ValidateString.Trim();
if (ValidateString.Length == 0)
{
return false;
}
else
{
Match m = Regex.Match(ValidateString, @"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$");
if (m.Success)
{
if (Convert.ToDouble(ValidateString) <= 100)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
}
/// <summary>
///判断输入框可为空,不为空时输入值为<=100正浮点数(百分比)
/// </summary>
//
public bool E_Percent(String ValidateString)
{
ValidateString = ValidateString.Trim();
if (ValidateString.Length == 0)
{
return true;
}
else
{
Match m = Regex.Match(ValidateString, @"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$");
if (m.Success)
{
if (Convert.ToDouble(ValidateString) <= 100)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
}
/// <summary>
/////判断输入框不能为空,并且输入值为6位数字的邮政编码
/// </summary>
public bool PostalCode(String ValidateString)
{
ValidateString = ValidateString.Trim();
if (ValidateString.Length != 6)
{
return false;
}
else
{
Match m = Regex.Match(ValidateString, @"^[0-9]+$");
if (m.Success)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/////判断输入框不能为空,并且输入值为6位数字的邮政编码
/// </summary>
public bool E_PostalCode(String ValidateString)
{
ValidateString = ValidateString.Trim();
if (ValidateString.Length == 0)
{
return true;
}
else
{
Match m = Regex.Match(ValidateString, @"^[0-9]+$");
if (m.Success)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/////处理数据,在把数据存到数据库前,先屏蔽那些危险字符!
/// </summary>
public string DealString(string str)
{
str = str + "1";
string str1 = str.ToLower();
string[] vv ={ "count", "*", "%", "chr", "mid", "master", "'", "<", ">", "\r", "\'", "\x0020", "@", ";", "and", "(", ")", "exec", "insert", "select", "delete", "update", "truncate", "char", "declare" };
for (int i = 0; i < vv.Length; i++)
{
while (str1.IndexOf(vv[i], 0, str1.Length - 1) != -1)
{
int bb = str1.IndexOf(vv[i], 0, str1.Length - 1);
str1 = str1.Remove(bb, vv[i].Length);
str = str.Remove(bb, vv[i].Length);
}
}
str = str.Remove(str.Length - 1, 1);
return (str);
}
/// <summary>
/////恢复数据:把数据库中的数据,还原成未处理前的样子!
/// </summary>
//
public string UnDealString(string str)
{
str = str.Replace("<", "<");
str = str.Replace(">", ">");
str = str.Replace("<br>", "\r");
str = str.Replace("’", "\'");
str = str.Replace(" ", "\x0020");
str = str.Replace("''", "'");
return (str);
}
/// <summary>
/// 判断:输入框不能为空,并且输入值为大雨零的整数
/// </summary>
//
public bool NumberExceptZero(String ValidateString)
{
ValidateString = ValidateString.Trim();
if (ValidateString.Length == 0)
{
return false;
}
else
{
Match m = Regex.Match(ValidateString, @"^[1-9]+$");
if (m.Success)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// 大于0小于50
/// </summary>
/// <param name="ValidateString"></param>
/// <returns></returns>
public bool StringLessThan50(string ValidateString)
{
if (ValidateString.Length <= 50 && ValidateString.Length >0)
{
return true ;
}
else
return false ;
}
/// <summary>
/// 大于0小于500
/// </summary>
/// <param name="ValidateString"></param>
/// <returns></returns>
public bool StringLessThan500(string ValidateString)
{
if (ValidateString.Length <=500 && ValidateString .Length >0)
{
return true ;
}
else
return false ;
}
}