• 我的数据验证类PageValidate[正则表达式]


    1. /*------------------------------------
      --公司:西安上古科技发展有限公司
      --项目名称:西安环太科技业务办公系统
      --用途:数据验证方法
      --创建日期:2007-11-22
      --创建人:周海军
      ------------------------------------*/
      using System;
      using System.Collections.Generic;
      using System.Text;
      using System.Text.RegularExpressions;
      using System.Web;
      using System.Web.UI.WebControls;
      namespace ShangGu.Utility
      {
          public class PageValidate
          {
              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 数字字符串检查

            /// <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="personID"></param>
            /// <returns></returns>
            public static bool PersonID(string personID)
            {
                string idval = @"^(\d{15})(\d{2}[Xx0-9])?$";
                return Regex.IsMatch(personID, idval);
            }

            /// <summary>
            /// 数字验证
            /// </summary>
            /// <param name="num"></param>
            /// <returns></returns>
            public static bool Number(string num)
            {
                string numval = @"^[0-9]*[1-9][0-9]*$";
                return Regex.IsMatch(num, numval);
            }

            /// <summary>
            /// 电话验证
            /// </summary>
            /// <param name="tel"></param>
            /// <returns></returns>
            public static bool Telphone(string tel)
            {
                string telaval = @"^0[1-9]\d{1,2}-[1-9]\d{6,7}$";
                string telval = @"^[1-9]\d{6,7}$";
                string modval = @"^(159|156|153|130|131|132|133|134|135|136|137|138|139)\d{8}$";
                return Regex.IsMatch(tel, telval) || Regex.IsMatch(tel, modval) || Regex.IsMatch(tel, telaval);
            }
            /// <summary>
            /// 验证日期格式(格式为YYYY-MM-dd)
            /// </summary>
            /// <param name="datetime"></param>
            /// <returns></returns>
            public static bool DateTimeValidate(string datetime)
            {
                string dateval =
                    @"(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)";
                return Regex.IsMatch(datetime, dateval);
            }
            /// <summary>
            /// 验证日期格式(格式为YYYY/MM/DD)
            /// </summary>
            /// <param name="datetime"></param>
            /// <returns></returns>
            public static bool DateTime_Validate(string datetime)
            {
                string dateval = "(((0[1-9]|[12][0-9]|3[01])/((0[13578]|1[02]))|((0[1-9]|[12][0-9]|30)/(0[469]|11))|(0[1-9]|[1][0-9]|2[0-8])/(02))/([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3}))|(29/02/(([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00)))";
                return Regex.IsMatch(datetime,dateval);
            }
            /// <summary>
            /// Email验证
            /// </summary>
            /// <param name="email"></param>
            /// <returns></returns>
            public static bool EmailValidate(string email)
            {
                string emailval = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
                return Regex.IsMatch(email, emailval);

            }

            /// <summary>
            /// 邮政编码验证
            /// </summary>
            /// <param name="PostCode"></param>
            /// <returns></returns>
            public static bool PostCodeValidate(string PostCode)
            {
                string postval = @"^[1-9]\d{5}$";
                return Regex.IsMatch(PostCode, postval);
            }

            /// <summary>
            /// 金额验证
            /// </summary>
            /// <param name="moneycheck"></param>
            /// <returns></returns>
            public static bool Money(string moneycheck)
            {
                string monval = @"^([1-9]\d+|[0-9])(\.\d\d?)*$";
                return Regex.IsMatch(moneycheck, monval);
            }
            #endregion

            #region 中文检测

    1.         /// <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="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());
            }
            #endregion
        }
    }

  • 相关阅读:
    TextBox类的成员
    【VB.Net】字节数与字符数的取得
    限制文本框的输入类型
    【转】FxCop 设计规则
    【Spread Sheet 应用(三)】Enter移向下一个单元格
    【ASP.NET】打开关闭新窗体
    常用字符对应ASCII码值
    初识CodeSmith分享两个模板
    【VB.NET】自定义控件(一)属性说明
    LeetCode: Length of Last Word
  • 原文地址:https://www.cnblogs.com/ziyan22/p/1088010.html
Copyright © 2020-2023  润新知