using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using System.Web.UI.HtmlControls; using System.Web.UI; using System.Text.RegularExpressions; namespace Common { public class Validate { private static readonly Regex RegPhone = new Regex("(^(\d{11})$|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)"); //電話號碼和手機驗證 private static Regex RegEmail = new Regex("^\s*([A-Za-z0-9_-]+(\.\w+)*@([\w-]+\.)+\w{2,3})\s*$"); //new Regex("^[\w-]+@[\w-]+\.(com|net|org|edu|mil|tv|biz|info)$");//w 英文字母或數字的字串,和 [a-zA-Z0-9] 語法一樣 private static Regex RegNum = new Regex("^[0-9]+$"); //必須是數字正規表示式(小數或整數) private static Regex regex = new Regex(@"/^[0-9]+.?[0-9]{0,3}$/"); /// <summary> /// 身份證正值表示式 /// </summary> private static readonly Regex RegCardId = new Regex("(^\d{15}$)|(^\d{17}([0-9]|X|x)$)"); #region 確定使用者輸入是否合法 /// <summary> /// 確定使用者輸入是否合法 /// </summary> /// <param name="text">使用者輸入字串</param> /// <param name="maxLength">最大字串長度</param> /// <returns></returns> public static string InputText(string text, int maxLength) { if (string.IsNullOrEmpty(text)) return string.Empty; text = text.Trim(); if (maxLength != 0) if (text.Length > maxLength) text = text.Substring(0, maxLength); text = Regex.Replace(text, "[\s]{2,}", " "); text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\n)*?>)", " "); text = Regex.Replace(text, "(\s*&[n|N][b|B][s|S][p|P];\s*)+", " "); //text = Regex.Replace(text, "<(.|\n)*?>", string.Empty); //遮蔽標籤 text = text.Replace("`", "``"); return text; } #endregion #region 驗證電話號碼 // 電話號碼和手機號碼檢查 /// <summary> /// 電話號碼和手機號碼檢查 /// </summary> /// <param name="inputData">電話號碼或手機號碼</param> /// <returns>匹配結果</returns> public static bool IsPhone(string inputData) { Match m = RegPhone.Match(inputData); return m.Success; } #endregion #region 驗證引數是否為中文字元 /// <summary> /// 驗證引數是否為中文字元 /// </summary> /// <param name="input">輸入引數</param> /// <returns></returns> public static bool IsChinese(string input) { Regex regex = new Regex(@"[u4e00-u9fa5]", RegexOptions.IgnoreCase); return regex.IsMatch(input); } #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 #region 是否為數字 /// <summary> /// 是否為數字 /// </summary> /// <param name="inputData">輸入字串</param> /// <returns>是否為數字</returns> public static bool IsNum(string inputData) { if(string.IsNullOrEmpty(inputData)) { return false; } Match m = RegNum.Match(inputData); return m.Success; } /// <summary> /// 判斷是否是整數或小數 /// </summary> /// <param name="str">輸入的字串</param> /// <returns>是否為數字</returns> public static bool IsNumAll(string str) { if (string.IsNullOrEmpty(str)) { return false; } Match m = regex.Match(str); return m.Success; } #endregion #region 是否為身份證 /// <summary> /// 是否為身份證 /// </summary> /// <param name="inputData">輸入字串</param> /// <returns>是否為身份證</returns> public static bool IsCardId(string inputData) { Match m = RegCardId.Match(inputData); return m.Success; } #endregion /// <summary> /// 判斷字串是否是純數字 /// </summary> /// <param name="message">源字串</param> /// <returns></returns> public static bool IsNumberic(string message)//, out int result { System.Text.RegularExpressions.Regex rex = new System.Text.RegularExpressions.Regex(@"^d+$"); var result = -1; if (rex.IsMatch(message)) { result = int.Parse(message); return true; } else return false; } } }