/DotNet.Framework.Common/PageValidate.cs
C# | 775 lines | 519 code | 74 blank | 182 comment | 96 complexity | 754780f5323beb64db33aa6da63dcc84 MD5 | raw file
- using System;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Web;
- using DotNet.Framework.Common.Helper;
-
- namespace DotNet.Framework.Common
- {
- /// <summary>
- /// 页面数据校验类
- /// 李天平
- /// 2004.8
- /// </summary>
- public class PageValidate
- {
- 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|cn|org|edu|mil|tv|biz|info)$");//w 英文字母或数字的字符串,和 [a-zA-Z0-9] 语法一样
- private static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]");
- /// <summary>
- /// 默认构造函数
- /// </summary>
- public PageValidate()
- {
- }
-
-
- #region 数字字符串检查
- /// <summary>
- /// 检查输入的字符串是否是电话类型
- /// </summary>
- /// <param name="inputData"></param>
- /// <returns></returns>
- 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="Id">身份证号码</param>
- /// <returns>验证成功为True,否则为False</returns>
- public static bool CheckIDCard(string Id)
- {
- if (Id.Length == 18)
- {
- bool check = CheckIDCard18(Id);
- return check;
- }
- else if (Id.Length == 15)
- {
- bool check = CheckIDCard15(Id);
- return check;
- }
- else
- {
- return false;
- }
- }
-
-
- /// <summary>
- /// 验证15位身份证号
- /// </summary>
- /// <param name="Id">身份证号</param>
- /// <returns>验证成功为True,否则为False</returns>
- private static bool CheckIDCard18(string Id)
- {
- long n = 0;
- if (long.TryParse(Id.Remove(17), out n) == false || n < Math.Pow(10, 16) || long.TryParse(Id.Replace('x', '0').Replace('X', '0'), out n) == false)
- {
- return false;//数字验证
- }
- string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
- if (address.IndexOf(Id.Remove(2)) == -1)
- {
- return false;//省份验证
- }
- string birth = Id.Substring(6, 8).Insert(6, "-").Insert(4, "-");
- DateTime time = new DateTime();
- if (DateTime.TryParse(birth, out time) == false)
- {
- return false;//生日验证
- }
- string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
- string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
- char[] Ai = Id.Remove(17).ToCharArray();
- int sum = 0;
- for (int i = 0; i < 17; i++)
- {
- sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());
- }
- int y = -1;
- Math.DivRem(sum, 11, out y);
- if (arrVarifyCode[y] != Id.Substring(17, 1).ToLower())
- {
- return false;//校验码验证
- }
- return true;//符合GB11643-1999标准
- }
-
- /// <summary>
- /// 验证18位身份证号
- /// </summary>
- /// <param name="Id">身份证号</param>
- /// <returns>验证成功为True,否则为False</returns>
- private static bool CheckIDCard15(string Id)
- {
- long n = 0;
- if (long.TryParse(Id, out n) == false || n < Math.Pow(10, 14))
- {
- return false;//数字验证
- }
- string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
- if (address.IndexOf(Id.Remove(2)) == -1)
- {
- return false;//省份验证
- }
- string birth = Id.Substring(6, 6).Insert(4, "-").Insert(2, "-");
- DateTime time = new DateTime();
- if (DateTime.TryParse(birth, out time) == false)
- {
- return false;//生日验证
- }
- return true;//符合15位身份证标准
- }
-
- #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(System.Web.UI.WebControls.Label lbl, string txtInput)
- {
- lbl.Text = HtmlEncode(txtInput);
- }
- /// <summary>
- /// 为Label赋值
- /// </summary>
- /// <param name="lbl"></param>
- /// <param name="inputObj"></param>
- public static void SetLabel(System.Web.UI.WebControls.Label lbl, object inputObj)
- {
- SetLabel(lbl, inputObj.ToString());
- }
- /// <summary>
- /// 字符串清理
- /// </summary>
- /// <param name="inputString"></param>
- /// <param name="maxLength"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// SQL注入字符清理
- /// </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 是否由特定字符组成
- 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
-
- #region 验证IP地址是否合法
- /// <summary>
- /// 验证IP地址是否合法
- /// </summary>
- /// <param name="ip">要验证的IP地址</param>
- public static bool IsIP(string ip)
- {
- //如果为空,认为验证合格
- if (IsNullOrEmpty(ip))
- {
- return true;
- }
-
- //清除要验证字符串中的空格
- ip = ip.Trim();
-
- //模式字符串
- string pattern = @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$";
-
- //验证
- return RegexHelper.IsMatch(ip, pattern);
- }
- #endregion
-
-
-
- #region 验证是否为整数
- /// <summary>
- /// 验证是否为整数 如果为空,认为验证不合格 返回false
- /// </summary>
- /// <param name="number">要验证的整数</param>
- public static bool IsInt(string number)
- {
- //如果为空,认为验证不合格
- if (IsNullOrEmpty(number))
- {
- return false;
- }
-
- //清除要验证字符串中的空格
- number = number.Trim();
-
- //模式字符串
- string pattern = @"^[0-9]+[0-9]*$";
-
- //验证
- return RegexHelper.IsMatch(number, pattern);
- }
- #endregion
-
-
-
- #region 验证日期是否合法
- /// <summary>
- /// 验证日期是否合法,对不规则的作了简单处理
- /// </summary>
- /// <param name="date">日期</param>
- public static bool IsDate(ref string date)
- {
- //如果为空,认为验证合格
- if (IsNullOrEmpty(date))
- {
- return true;
- }
-
- //清除要验证字符串中的空格
- date = date.Trim();
-
- //替换\
- date = date.Replace(@"\", "-");
- //替换/
- date = date.Replace(@"/", "-");
-
- //如果查找到汉字"今",则认为是当前日期
- if (date.IndexOf("今") != -1)
- {
- date = DateTime.Now.ToString();
- }
-
- try
- {
- //用转换测试是否为规则的日期字符
- date = Convert.ToDateTime(date).ToString("d");
- return true;
- }
- catch
- {
- //如果日期字符串中存在非数字,则返回false
- if (!IsInt(date))
- {
- return false;
- }
-
- #region 对纯数字进行解析
- //对8位纯数字进行解析
- if (date.Length == 8)
- {
- //获取年月日
- string year = date.Substring(0, 4);
- string month = date.Substring(4, 2);
- string day = date.Substring(6, 2);
-
- //验证合法性
- if (Convert.ToInt32(year) < 1900 || Convert.ToInt32(year) > 2100)
- {
- return false;
- }
- if (Convert.ToInt32(month) > 12 || Convert.ToInt32(day) > 31)
- {
- return false;
- }
-
- //拼接日期
- date = Convert.ToDateTime(year + "-" + month + "-" + day).ToString("d");
- return true;
- }
-
- //对6位纯数字进行解析
- if (date.Length == 6)
- {
- //获取年月
- string year = date.Substring(0, 4);
- string month = date.Substring(4, 2);
-
- //验证合法性
- if (Convert.ToInt32(year) < 1900 || Convert.ToInt32(year) > 2100)
- {
- return false;
- }
- if (Convert.ToInt32(month) > 12)
- {
- return false;
- }
-
- //拼接日期
- date = Convert.ToDateTime(year + "-" + month).ToString("d");
- return true;
- }
-
- //对5位纯数字进行解析
- if (date.Length == 5)
- {
- //获取年月
- string year = date.Substring(0, 4);
- string month = date.Substring(4, 1);
-
- //验证合法性
- if (Convert.ToInt32(year) < 1900 || Convert.ToInt32(year) > 2100)
- {
- return false;
- }
-
- //拼接日期
- date = year + "-" + month;
- return true;
- }
-
- //对4位纯数字进行解析
- if (date.Length == 4)
- {
- //获取年
- string year = date.Substring(0, 4);
-
- //验证合法性
- if (Convert.ToInt32(year) < 1900 || Convert.ToInt32(year) > 2100)
- {
- return false;
- }
-
- //拼接日期
- date = Convert.ToDateTime(year).ToString("d");
- return true;
- }
- #endregion
-
- return false;
- }
- }
- #endregion
-
- #region 验证身份证是否合法
- /// <summary>
- /// 验证身份证是否合法
- /// </summary>
- /// <param name="idCard">要验证的身份证</param>
- public static bool IsIdCard(string idCard)
- {
- //如果为空,认为验证合格
- if (IsNullOrEmpty(idCard))
- {
- return true;
- }
-
- //清除要验证字符串中的空格
- idCard = idCard.Trim();
-
- //模式字符串
- StringBuilder pattern = new StringBuilder();
- pattern.Append(@"^(11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|");
- pattern.Append(@"50|51|52|53|54|61|62|63|64|65|71|81|82|91)");
- pattern.Append(@"(\d{13}|\d{15}[\dx])$");
-
- //验证
- return RegexHelper.IsMatch(idCard, pattern.ToString());
- }
- #endregion
-
- #region 检测客户的输入中是否有危险字符串
- /// <summary>
- /// 检测客户输入的字符串是否有效,并将原始字符串修改为有效字符串或空字符串。
- /// 当检测到客户的输入中有攻击性危险字符串,则返回false,有效返回true。
- /// </summary>
- /// <param name="input">要检测的字符串</param>
- public static bool IsValidInput(ref string input)
- {
- try
- {
- if (IsNullOrEmpty(input))
- {
- //如果是空值,则跳出
- return true;
- }
- else
- {
- //替换单引号
- input = input.Replace("'", "''").Trim();
-
- //检测攻击性危险字符串
- string testString = "and |or |exec |insert |select |delete |update |count |chr |mid |master |truncate |char |declare ";
- string[] testArray = testString.Split('|');
- foreach (string testStr in testArray)
- {
- if (input.ToLower().IndexOf(testStr) != -1)
- {
- //检测到攻击字符串,清空传入的值
- input = "";
- return false;
- }
- }
-
- //未检测到攻击字符串
- return true;
- }
- }
- catch (Exception ex)
- {
- throw new Exception(ex.Message);
- }
- }
- #endregion
-
- #region 判断对象是否为空
- /// <summary>
- /// 判断对象是否为空,为空返回true
- /// </summary>
- /// <typeparam name="T">要验证的对象的类型</typeparam>
- /// <param name="data">要验证的对象</param>
- public static bool IsNullOrEmpty<T>(T data)
- {
- //如果为null
- if (data == null)
- {
- return true;
- }
-
- //如果为""
- if (data.GetType() == typeof(String))
- {
- if (string.IsNullOrEmpty(data.ToString().Trim()))
- {
- return true;
- }
- }
-
- //如果为DBNull
- if (data.GetType() == typeof(DBNull))
- {
- return true;
- }
-
- //不为空
- return false;
- }
-
- /// <summary>
- /// 判断对象是否为空,为空返回true
- /// </summary>
- /// <param name="data">要验证的对象</param>
- public static bool IsNullOrEmpty(object data)
- {
- //如果为null
- if (data == null)
- {
- return true;
- }
-
- //如果为""
- if (data.GetType() == typeof(String))
- {
- if (string.IsNullOrEmpty(data.ToString().Trim()))
- {
- return true;
- }
- }
-
- //如果为DBNull
- if (data.GetType() == typeof(DBNull))
- {
- return true;
- }
-
- //不为空
- return false;
- }
- #endregion
- }
- }