using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace Utility.UI { ///<summary> ///<Author>jilongliang</Author> ///<Date>2012/9/7</Date> ///<Email >jilongliang@sina.com</Email > ///<Description>验证类</Description> ///</summary> public class ValidateHelper { 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]");//中文 private static Regex RegQQ = new Regex("[1-9][0-9]{4,}");//匹配腾讯QQ号 private static Regex RegTel = new Regex("d{3}-d{8}|d{4}-d{7}");//Tel匹配国内电话号码 private static Regex RegURL = new Regex("[a-zA-z]+://[^s]*");//URL private static Regex RegZipCode = new Regex("[1-9]d{5}(?!d)");//CN 邮政编码 private static Regex RegIDCard = new Regex("d{15}|d{18}");//匹配身份证:d{15}|d{18} private static Regex RegHTML = new Regex("<(S*?)[^>]*>.*?|<.*? />");//匹配HTML标记的正则表达式:<(S*?)[^>]*>.*?|<.*? /> private static Regex RegPassWord = new Regex("^[a-zA-Z]w{5,17}$"); //验证用户密码:“^[a-zA-Z]w{5,17}$”正确格式为:以字母开头,长度在6-18之间 private static Regex Reg26A_Z = new Regex("^[A-Za-z]+$");//只能输入由26个英文字母组成的字符串:“^[A-Za-z]+$” private static Regex Reg26Large_A_Z = new Regex("^[A-Z]+$");//只能输入由26个大写英文字母组成的字符串:“^[A-Z]+$” private static Regex Reg26Small_A_Z = new Regex("^[a-z]+$");//只能输入由26个小写英文字母组成的字符串:“^[a-z]+$” private static Regex Reg0_9_A_Z = new Regex("^[A-Za-z0-9]+$");//只能输入由数字和26个英文字母组成的字符串:“^[A-Za-z0-9]+$” private static Regex Reg0_9_A_Z_ = new Regex("^w+$");//只能输入由数字、26个英文字母或者下划线组成的字符串:“^w+$” ///<summary> ///无参数构造方法 ///</summary> public ValidateHelper() { } ///<summary> ///是否数字字符串 ///</summary> ///<param name="inputData">输入字符串</param> ///<returns></returns> public static bool IsNumber(string inputData) { Match m = RegNumber.Match(inputData); return m.Success; } ///<summary> ///是否数字字符串 可带正负号 ///</summary> ///<param name="inputData">输入字符串</param> ///<returns></returns> public static bool IsNumberSign(string inputData) { Match m = RegNumberSign.Match(inputData); return m.Success; } ///<summary> ///是否是浮点数 ///</summary> ///<param name="inputData">输入字符串</param> ///<returns></returns> public static bool IsDecimal(string inputData) { Match m = RegDecimal.Match(inputData); return m.Success; } ///<summary> ///是否是浮点数 可带正负号 ///</summary> ///<param name="inputData">输入字符串</param> ///<returns></returns> public static bool IsDecimalSign(string inputData) { Match m = RegDecimalSign.Match(inputData); return m.Success; } #region中文检测 ///<summary> ///检测是否有中文字符 ///</summary> ///<param name="inputData"></param> ///<returns></returns> public static bool IsHasCHZN(string inputData) { Match m = RegCHZN.Match(inputData); return m.Success; } #endregion #region邮件地址 ///<summary> ///是否是浮点数 可带正负号 ///</summary> ///<param name="inputData">输入字符串</param> ///<returns></returns> public static bool IsEmail(string inputData) { Match m = RegEmail.Match(inputData); return m.Success; } #endregion ///<summary> ///验证是否合法的URL ///</summary> ///<param name="InputStr"></param> ///<returns></returns> public static bool IsURL(string InputStr) { Match m = RegURL.Match(InputStr); return m.Success; } ///<summary> ///验证电话号码是否合法 ///</summary> ///<param name="InputStr"></param> ///<returns></returns> public static bool IsTelNumber(string InputStr) { Match m = RegTel.Match(InputStr); return m.Success; } ///<summary> ///验证邮政编码 ///</summary> ///<param name="InputStr"></param> ///<returns></returns> public static bool IsZipCode(string InputStr) { Match m = RegZipCode.Match(InputStr); return m.Success; } ///<summary> ///验证QQ号 ///</summary> ///<param name="InputStr"></param> ///<returns></returns> public static bool IsQQ(string InputStr) { Match m = RegQQ.Match(InputStr); return m.Success; } ///<summary> ///验证身份证 ///</summary> ///<param name="InputStr"></param> ///<returns></returns> public static bool IsIDCard(string InputStr) { Match m = RegIDCard.Match(InputStr); return m.Success; } ///<summary> ///验证用户密码 ///</summary> ///<param name="InputStr"></param> ///<returns></returns> public static bool IsPassWord(string InputStr) { Match m = RegPassWord.Match(InputStr); return m.Success; } ///<summary> ///验证只能输入由26个英文字母组成的字符串 ///</summary> ///<param name="InputStr"></param> ///<returns></returns> public static bool Is26A_Z(string InputStr) { Match m = Reg26A_Z.Match(InputStr); return m.Success; } ///<summary> ///只能输入由26个大写英文字母组成的字符串 ///</summary> ///<param name="InputStr"></param> ///<returns></returns> public static bool Is26Large_A_Z(string InputStr) { Match m = Reg26Large_A_Z.Match(InputStr); return m.Success; } ///<summary> ///只能输入由26个小写英文字母组成的字符串 ///</summary> ///<param name="InputStr"></param> ///<returns></returns> public static bool Is26Small_A_Z(string InputStr) { Match m = Reg26Small_A_Z.Match(InputStr); return m.Success; } ///<summary> ///只能输入由数字和26个英文字母组成的字符串 ///</summary> ///<param name="InputStr"></param> ///<returns></returns> public static bool Is0_9_AZ_Str(string InputStr) { Match m = Reg0_9_A_Z.Match(InputStr); return m.Success; } ///<summary> ///只能输入由数字和26个英文字母组成的字符串和下划线 ///</summary> ///<param name="InputStr"></param> ///<returns></returns> public static bool Is0_9_A_Z_Str(string InputStr) { Match m = Reg0_9_A_Z_.Match(InputStr); return m.Success; } } }
通用验证类Validate
最新推荐文章于 2024-08-27 19:48:04 发布