123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Web;
- namespace SysBaseLibs
- {
- public sealed class ValidUtils
- {
- private static Regex RegPhone = new Regex("^[0-9]+[-]?[0-9]+[-]?[0-9]$");
- 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]");
- #region 数字字符串检查
- public static bool IsPhone(string inputData)
- {
- Match m = RegPhone.Match(inputData);
- return m.Success;
- }
- /// <summary>
- /// 检查Request查询字符串的键值,是否是数字,最大长度限制
- /// </summary>
- /// <param name="req">Request</param>
- /// <param name="inputKey">Request的键值</param>
- /// <param name="maxLen">最大长度</param>
- /// <returns>返回Request查询字符串</returns>
- public static string FetchInputDigit(HttpRequest req, string inputKey, int maxLen)
- {
- string retVal = string.Empty;
- if (inputKey != null && inputKey != string.Empty)
- {
- retVal = req.QueryString[inputKey];
- if (null == retVal)
- retVal = req.Form[inputKey];
- if (null != retVal)
- {
- retVal = SqlText(retVal, maxLen);
- if (!IsNumber(retVal))
- retVal = string.Empty;
- }
- }
- if (retVal == null)
- retVal = string.Empty;
- return retVal;
- }
- /// <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;
- }
- #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 日期格式判断
- /// <summary>
- /// 日期格式字符串判断
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static bool IsDateTime(string str)
- {
- try
- {
- if (!string.IsNullOrEmpty(str))
- {
- DateTime.Parse(str);
- return true;
- }
- else
- {
- return false;
- }
- }
- catch
- {
- return false;
- }
- }
- #endregion
- #region 其他
- /// <summary>
- /// 检查字符串最大长度,返回指定长度的串
- /// </summary>
- /// <param name="sqlInput">输入字符串</param>
- /// <param name="maxLength">最大长度</param>
- /// <returns></returns>
- public static string SqlText(string sqlInput, int maxLength)
- {
- if (sqlInput != null && sqlInput != string.Empty)
- {
- sqlInput = sqlInput.Trim();
- if (sqlInput.Length > maxLength)//按最大长度截取字符串
- sqlInput = sqlInput.Substring(0, maxLength);
- }
- return sqlInput;
- }
- /// <summary>
- /// 字符串编码
- /// </summary>
- /// <param name="inputData"></param>
- /// <returns></returns>
- public static string HtmlEncode(string inputData)
- {
- return HttpUtility.HtmlEncode(inputData);
- }
- ///// <summary>
- ///// 设置Label显示Encode的字符串
- ///// </summary>
- ///// <param name="lbl"></param>
- ///// <param name="txtInput"></param>
- //public static void SetLabel(Label lbl, string txtInput)
- //{
- // lbl.Text = HtmlEncode(txtInput);
- //}
- //public static void SetLabel(Label lbl, object inputObj)
- //{
- // SetLabel(lbl, inputObj.ToString());
- //}
- //字符串清理
- public static string InputText(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();
- }
- /// <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;
- }
- 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 是否由特定字符组成
- public static bool isContainSameChar(string strInput)
- {
- string charInput = string.Empty;
- if (!string.IsNullOrEmpty(strInput))
- {
- charInput = strInput.Substring(0, 1);
- }
- return isContainSameChar(strInput, charInput, strInput.Length);
- }
- public static bool isContainSameChar(string strInput, string charInput, int lenInput)
- {
- 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>
- 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
- public static bool isEmail(string Email)
- {
- bool flag = true;
- string pattern = "/^[a-zA-Z0-9_-]*@[a-zA-Z0-9_-]/.[a-zA-Z0-9_-]";
- Regex regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.IgnoreCase);
- if (regex.Match(Email).Success)
- {
- flag = true;
- }
- return flag;
- }
- public static bool isIDCard(string idNum, string birthdayYear, int Gender)
- {
- bool flag = false;
- string input = idNum;
- string str2 = "";
- string s = "";
- int length = input.Length;
- if ((length == 15) && (length == 0x12))
- {
- string pattern = "";
- switch (length)
- {
- case 15:
- pattern = "/d{15}";
- str2 = "19" + input.Substring(6, 2);
- s = input.Substring(14, 1);
- break;
- case 0x12:
- pattern = "/d{17}[0-9x]";
- str2 = input.Substring(6, 4);
- s = input.Substring(0x10, 1);
- break;
- }
- Regex regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.IgnoreCase);
- if (!regex.Match(input).Success)
- {
- return flag;
- }
- if ((birthdayYear.Length > 0) && (birthdayYear != str2))
- {
- return flag;
- }
- if (Gender == -1)
- {
- return true;
- }
- if ((int.Parse(s) % 2) == 0)
- {
- if (Gender != 0)
- {
- return flag;
- }
- return flag;
- }
- if (Gender != 1)
- {
- return flag;
- }
- }
- return flag;
- }
- }
- }
|