郑德才博客 记录学习,记录工作,学习知识分享!

Asp.Net中常用的数据验证类

   /// <summary>

   /// 数据验证类

   /// </summary>

   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]");


       public DataVerification()

       {

           //

           // 无参数构造函数

           //

       }


       #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

   }

2013年3月25日 | 发布:郑德才博客 | 分类:学习之路 | 评论:4

留言列表:

  • 入门啦 发布于 2013/3/26 19:59:26  回复
  • 博主的文章都是自己写的吗?写的挺好的,也挺受益,谢谢
  • 网络设备批发网 发布于 2013/3/28 9:40:26  回复
  • 博主很有才啊 不是一般的厉害
  • 路由器 发布于 2013/3/28 9:41:16  回复
  • 受益匪浅啊
  • 郑德才博客 发布于 2013/3/30 13:23:04  回复
  • 部分网上看着不错的就弄下来了

发表留言: