using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
using System.Web.Caching;
namespace WebApplication1
{
public class checkform
{
private static string emial = @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
private static string phone = @"(\(\d{3}\)|\d{3}-)?\d{8}";
private static string url = @"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?";
private static string idcart = @"\d{17}[\d|X]|\d{15}";
private static string mobile = @"^((\(\d{3}\))|(\d{3}\-))?13\d{9}$";
private static string currency = @"^\d+(\.\d+)?$";
private static string number = @"^\d+$";
private static string zip = @"^[1-9]\d{5}$";
private static string qq = @"^[1-9]\d{4,8}$";
private static string integer = @"^[-\+]?\d+$";
private static string doubles = @"^[-\+]?\d+(\.\d+)?$";
private static string english = @"^[A-Za-z]+$";
private static string chinese = @"^[\u0391-\uFFE5]+$";
private static string unsafes = "[~!@#$%^&*()=+[\\]{}''\";:/?.,><`|!·¥…—()\\-、;:。,》《]";
private static Regex reg;
private static Cache cache = HttpRuntime.Cache;
public enum RegexType
{
/// <summary>
/// 电子邮件
/// </summary>
Email,
/// <summary>
/// 电话号码
/// </summary>
Phone,
/// <summary>
/// 连接地址
/// </summary>
Url,
/// <summary>
/// 身份证号
/// </summary>
IdCart,
/// <summary>
/// 手机号码
/// </summary>
Mobile,
/// <summary>
/// 货币
/// </summary>
Currency,
/// <summary>
/// 数值
/// </summary>
Number,
/// <summary>
/// 邮政编码
/// </summary>
Zip,
/// <summary>
/// QQ号码
/// </summary>
QQ,
/// <summary>
/// 整数
/// </summary>
Integer,
/// <summary>
/// 双精度类型
/// </summary>
Double,
/// <summary>
/// 英文
/// </summary>
English,
/// <summary>
/// 中文
/// </summary>
Chinese,
/// <summary>
/// 特殊字符
/// </summary>
UnSafe
}
public static bool IsMatch(RegexType regtype, string txt, bool isNull)
{
if (isNull && txt.Length == 0) return true;
//如果缓存中不存在验证表达式,那就将验证表达式添加到缓存中
try
{
bool checktype = bool.Parse(cache["CheckType"].ToString());
}
catch
{
cache.Insert(RegexType.Email.ToString(), emial);
cache.Insert(RegexType.Chinese.ToString(), chinese);
cache.Insert(RegexType.Currency.ToString(), currency);
cache.Insert(RegexType.Double.ToString(), doubles);
cache.Insert(RegexType.English.ToString(), english);
cache.Insert(RegexType.IdCart.ToString(), idcart);
cache.Insert(RegexType.Integer.ToString(), integer);
cache.Insert(RegexType.Mobile.ToString(), mobile);
cache.Insert(RegexType.Number.ToString(), number);
cache.Insert(RegexType.Phone.ToString(), phone);
cache.Insert(RegexType.QQ.ToString(), qq);
cache.Insert(RegexType.UnSafe.ToString(), unsafes);
cache.Insert(RegexType.Url.ToString(), url);
cache.Insert(RegexType.Zip.ToString(), zip);
cache.Insert("CheckType", true);
}
bool ismat = false;
//从缓存中获取表达式
reg = new Regex(cache[regtype.ToString()].ToString());
ismat = reg.IsMatch(txt);
reg = null;
return ismat;
}
public static bool IsMatch(RegexType regtype, string txt, int size, bool isNull)
{
if (isNull && txt.Length == 0) return true;
if (txt.Length > size) return false;
//如果缓存中不存在验证表达式,那就将验证表达式添加到缓存中
try
{
bool checktype = bool.Parse(cache["CheckType"].ToString());
}
catch
{
cache.Insert(RegexType.Email.ToString(), emial);
cache.Insert(RegexType.Chinese.ToString(), chinese);
cache.Insert(RegexType.Currency.ToString(), currency);
cache.Insert(RegexType.Double.ToString(), doubles);
cache.Insert(RegexType.English.ToString(), english);
cache.Insert(RegexType.IdCart.ToString(), idcart);
cache.Insert(RegexType.Integer.ToString(), integer);
cache.Insert(RegexType.Mobile.ToString(), mobile);
cache.Insert(RegexType.Number.ToString(), number);
cache.Insert(RegexType.Phone.ToString(), phone);
cache.Insert(RegexType.QQ.ToString(), qq);
cache.Insert(RegexType.UnSafe.ToString(), unsafes);
cache.Insert(RegexType.Url.ToString(), url);
cache.Insert(RegexType.Zip.ToString(), zip);
cache.Insert("CheckType", true);
}
bool ismat = false;
//从缓存中获取表达式
reg = new Regex(cache[regtype.ToString()].ToString());
ismat = reg.IsMatch(txt);
reg = null;
return ismat;
}
/// <summary>
/// 自定义验证
/// </summary>
/// <param name="pattern">自定义验证表达式</param>
/// <param name="txt"> 要验证的类型</param>
/// <returns></returns>
public static bool CustomMatch(string pattern, string txt)
{
bool ismat = false;
reg = new Regex(pattern);
ismat = reg.IsMatch(txt);
reg = null;
return ismat;
}
}
}
------测试代码
protected void Button1_Click(object sender, EventArgs e)
{
if (checkform.IsMatch(checkform.RegexType.English, this.txtstr.Text.Trim(), false))
{
}
else {
Response.Write("错误!");
}
}
}
}