using System; using System.Text.RegularExpressions; namespace SiaSun.LMS.Common { /// /// RegexValid 正则表达式匹配类 /// public class RegexValid { #region ------匹配规则字符串 /// /// 验证Email格式 /// static public string Email = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"; /// /// 验证出生日期 /// static public string Brith = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"; /// /// 验证URL /// static public string URL = @"^\s*(\d{4})-(\d{2})-(\d{2})\s*$"; /// /// 验证用户密码 /// static public string Pwd = @"^[a-zA-Z]\w{5,17}$"; /// /// 只能输入汉字 /// static public string Chinese = @"^[\u4e00-\u9fa5]{0,}$"; /// /// 验证是否为整数 /// static public string Integer = @"^[0-9]*$"; /// /// 只能输入非零的正整数 /// static public string Positive = @"^\+?[1-9][0-9]*$"; /// /// 只能输入由 26 个英文字母组成的字符串 /// static public string LetterStr = @"^[A-Za-z]+$"; /// /// 只能输入由数字和 26 个英文字母组成的字符串 /// static public string NumLetterStr = @"^[A-Za-z0-9]+$"; #endregion #region --------网络应用 /// /// 验证Email格式 /// /// 验证字符串 /// 合法返回true,否则返回false public static bool IsValidEmai(string strEmail) { return Regex.IsMatch(strEmail, Email); } /// /// 验证出生日期 /// /// 验证字符串 /// 合法返回true,否则返回false public static bool IsValidBirth(string strBirth) { return Regex.IsMatch(strBirth, Brith); } /// /// 验证URL /// /// 验证字符串 /// 合法返回true,否则返回false public static bool IsValidURL(string strURL) { return Regex.IsMatch(strURL,URL); } /// /// 验证用户密码,以字母开头,长度在6~18之间,只能包含字符、数字和下划线 /// /// 验证字符串 /// 合法返回true,否则返回false public static bool IsValidPassword(string str1) { return Regex.IsMatch(str1, Pwd); } /// /// 只能输入汉字 /// /// 验证字符串 /// 合法返回true,否则返回false public static bool IsValidChinese(string str1) { return Regex.IsMatch(str1, Chinese); } #endregion #region -----数字验证 /// /// 验证是否为整数 /// /// 验证字符串 /// 合法返回true,否则返回false public static bool IsValidInteger(string str1) { return Regex.IsMatch(str1,Integer ); } /// /// 只能输入非零的正整数 /// /// 验证字符串 /// 合法返回true,否则返回false public static bool IsValidPositive(string str1) { return Regex.IsMatch(str1, Positive); } #endregion #region -------字符串验证 /// /// 只能输入由 26 个英文字母组成的字符串 /// /// 验证字符串 /// 合法返回true,否则返回false public static bool IsValidLetterStr(string str1) { return Regex.IsMatch(str1, LetterStr); } /// /// 只能输入由数字和 26 个英文字母组成的字符串 /// /// 验证字符串 /// 合法返回true,否则返回false public static bool IsValidNumLetterStr(string str1) { return Regex.IsMatch(str1, NumLetterStr); } /// /// 使用正则表达式验证字符串 /// /// 待验证字符串 /// 配置文件中正则表达式的ID /// 合法返回true,否则返回false public static bool GetCodeCheck(string strCode, string strAppItem) { string strCheck = SiaSun.LMS.Common.StringUtil.GetConfig(strAppItem); Match mCode = Regex.Match(strCode, strCheck); if (mCode.Success) { return true; } else { return false; } } #endregion /// /// 判断文本是否符合匹配模式 /// public static bool IsValidate(string Text,string MatchModel) { return Regex.IsMatch(Text, MatchModel); } } }