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 >[email protected]</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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.