using System; using System.Collections.Generic; using System.Text; using System.Web; using System.Web.UI.WebControls; using System.Text.RegularExpressions; namespace Tools.Common { /// <summary> /// 页面验证类 /// </summary> public class DataValidate { private static Regex RegPhone = new Regex(@"^(1\d{10})|(\d{3,4}[-]\d{6,8})$"); /// <summary> /// 纯数字,无正负号 /// </summary> private static Regex RegNumber = new Regex("^[0-9]+$"); /// <summary> /// 纯数字,可能有正负号 /// </summary> private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+$"); /// <summary> /// 可能有有小数点的数字 /// </summary> private static Regex RegDecimal = new Regex(@"^(\d+[.]\d+)|(\d+)$"); /// <summary> /// 可能有小数点,也有可能有正负号的数字 /// </summary> private static Regex RegDecimalSign = new Regex(@"^[+-]?((\d+[.]\d+)|(\d+))$"); //等价于^[+-]?\d+[.]?\d+$ /// <summary> /// Email地址 /// </summary> private static Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info|com.cn)$"); /// <summary> /// 是否有中文 /// </summary> private static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]"); /// <summary> /// 是否为int数据 /// </summary> private static Regex RegInt = new Regex(@"^(-){0,1}\d+$"); #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 获取Request请求字符串的键值 /// <summary> /// 获取Request请求字符串的键值 /// </summary> /// <param name="req">Request</param> /// <param name="inputKey">Request的键值</param> /// <returns>返回Request请求字符串</returns> public static string GetRequest(HttpRequest req, string inputKey) { string retVal = string.Empty; if (inputKey != null && inputKey != string.Empty) { retVal = req.QueryString[inputKey]; if (null == retVal) retVal = req.Form[inputKey]; } if (retVal == null) retVal = string.Empty; return retVal; } #endregion #region 是否数字字符串 /// <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; } #endregion #region 是否是浮点数 /// <summary> /// 是否是浮点数 /// </summary> /// <param name="inputData">输入字符串</param> /// <returns></returns> public static bool IsDecimal(string inputData) { decimal d1; return decimal.TryParse(inputData, out d1); } /// <summary> /// 是否是浮点数 可带正负号 /// </summary> /// <param name="inputData">输入字符串</param> /// <returns></returns> public static bool IsDecimalSign(string inputData) { Match m = RegDecimalSign.Match(inputData); return m.Success; } #endregion #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 #region 字符串编码HtmlEncode /// <summary> /// 字符串编码HtmlEncode /// </summary> /// <param name="inputData"></param> /// <returns></returns> public static string HtmlEncode(string inputData) { return HttpUtility.HtmlEncode(inputData); } #endregion #region 设置Label显示Encode的字符串 /// <summary> /// 设置Label显示Encode的字符串 /// </summary> /// <param name="lbl"></param> /// <param name="txtInput"></param> public static void SetLabel(Label lbl, string txtInput) { lbl.Text = HtmlEncode(txtInput); } #endregion #region 字符串清理 /// <summary> /// 字符串清理 /// </summary> /// <param name="inputString">输入的字符串</param> /// <param name="maxLength">保留的长度</param> /// <returns></returns> public static string ClearText(string inputString, int maxLength) { StringBuilder retVal = new StringBuilder(); // 检查是否为空 if ((inputString != null) && (inputString != String.Empty)) { inputString = inputString.Trim(); //检查长度 if (inputString.Length > maxLength) { inputString = inputString.Substring(0, maxLength); } //替换危险字符 for (int i = 0; i < inputString.Length; i++) { switch (inputString[i]) { case '"': retVal.Append("""); break; case '<': retVal.Append("<"); break; case '>': retVal.Append(">"); break; default: retVal.Append(inputString[i]); break; } } retVal.Replace("'", " ");// 替换单引号 } return retVal.ToString(); } #endregion #region HTML Encode/Decode /// <summary> /// 转换成 HTML code /// </summary> /// <param name="str">string</param> /// <returns>string</returns> public static string Encode(string str) { str = str.Replace("&", "&"); str = str.Replace("'", "''"); str = str.Replace("\"", """); str = str.Replace(" ", " "); str = str.Replace("<", "<"); str = str.Replace(">", ">"); str = str.Replace("\n", "<br>"); return str; } /// <summary> ///解析html成 普通文本 /// </summary> /// <param name="str">string</param> /// <returns>string</returns> public static string Decode(string str) { str = str.Replace("<br>", "\n"); str = str.Replace(">", ">"); str = str.Replace("<", "<"); str = str.Replace(" ", " "); str = str.Replace(""", "\""); return str; } #endregion #region 清理字符串 /// <summary> /// 清理字符串 /// </summary> /// <param name="sqlText"></param> /// <returns></returns> public static string SqlTextClear(string sqlText) { if (sqlText == null) { return null; } if (sqlText == "") { return ""; } sqlText = sqlText.Replace(",", "");//去除, sqlText = sqlText.Replace("<", "");//去除< sqlText = sqlText.Replace(">", "");//去除> sqlText = sqlText.Replace("--", "");//去除-- sqlText = sqlText.Replace("'", "");//去除' sqlText = sqlText.Replace("\"", "");//去除" sqlText = sqlText.Replace("=", "");//去除= sqlText = sqlText.Replace("%", "");//去除% sqlText = sqlText.Replace(" ", "");//去除空格 return sqlText; } #endregion #region 是否由特定字符组成 /// <summary> /// 是否由特定字符组成 /// </summary> /// <param name="strInput">要检测的字符串</param> /// <returns></returns> public static bool isContainSameChar(string strInput) { string charInput = string.Empty; if (!string.IsNullOrEmpty(strInput)) { charInput = strInput.Substring(0, 1); } return isContainSameChar(strInput, charInput); } /// <summary> /// 是否由特定字符组成2 /// </summary> /// <param name="strInput">要检测的字符串</param> /// <param name="charInput">是否包含的字符</param> /// <returns></returns> public static bool isContainSameChar(string strInput, string charInput) { if (string.IsNullOrEmpty(charInput)) { return false; } else { Regex RegNumber = new Regex(string.Format("^([{0}])+$", charInput)); //Regex RegNumber = new Regex(string.Format("^([{0}]{{1}})+$", charInput,lenInput)); Match m = RegNumber.Match(strInput); return m.Success; } } #endregion #region 检查输入的是不是某些定义好的字符 /// <summary> /// 检查输入的是不是某些定义好的字符:用于密码输入的安全检查 /// </summary> /// <param name="strInput">要检测的字符串</param> /// <returns></returns> public static bool isContainSpecChar(string strInput) { string[] list = new string[] { "123456", "654321" }; bool result = new bool(); for (int i = 0; i < list.Length; i++) { if (strInput == list[i]) { result = true; break; } } return result; } #endregion #region 是不是int范围数据 /// <summary> /// 是不是int范围数据 /// </summary> /// <param name="inputData"></param> /// <returns></returns> public static bool IsInt(string inputData) { if (RegInt.Match(inputData).Success) { if ((long.Parse(inputData) > 0x7fffffffL) || (long.Parse(inputData) < -2147483648L)) { return false; } return true; } return false; } #endregion #region 是否是邮编号码 /// <summary> /// 是否是邮编号码 /// </summary> /// <param name="inputData"></param> /// <returns></returns> public static bool IsPostCode(string inputData) { return Regex.IsMatch(inputData, @"^\d{6}$", RegexOptions.IgnoreCase); } #endregion #region 是不是字母、数字、下划线的组合 /// <summary> /// 是不是字母、数字、下划线的组合 /// </summary> /// <param name="inputData"></param> /// <returns></returns> public static bool IsNormalChar(string inputData) { return Regex.IsMatch(inputData, @"[\w\d_]+", RegexOptions.IgnoreCase); } #endregion #region 是不是日期类型 /// <summary> /// 是不是日期类型 /// </summary> /// <param name="inputData"></param> /// <returns></returns> public static bool IsDataTime(string inputData) { DateTime dt1; return DateTime.TryParse(inputData, out dt1); } #endregion } }