友情链接: 互动百科 CSDN.NET 百度音乐 和讯理财 世界杯吧 拉手网

C#常用数据检查类

  1. using System;  
  2. using System.Text;  
  3. using System.Web;  
  4. using System.Web.UI.WebControls;  
  5. using System.Text.RegularExpressions;  
  6. namespace Legalsoft.Wizard.Basic // 设置项目属性可修改本项目的命名空间  
  7. {  
  8.     /// <summary>  
  9.     /// 页面数据校验类  
  10.     /// 修改自 李天平先生的作品,一并感谢。  
  11.     /// </summary>  
  12.     public class PageValidate  
  13.     {  
  14.         private static Regex RegNumber = new Regex("^[0-9]+$");  
  15.         private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+$");  
  16.         private static Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$");  
  17.         //等价于^[+-]?\d+[.]?\d+$  
  18.         private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$");   
  19.         //w 英文字母或数字的字符串,和 [a- zA-Z0-9] 语法一样   
  20.         private static Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");  
  21.         private static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]");  
  22.         public PageValidate()  
  23.         {  
  24.         }  
  25.         #region 数字字符串检 查       
  26.         /// <summary>  
  27.         /// 检查Request查询字符串的键值,是 否是数字,最大长度限制  
  28.         /// </summary>  
  29.         /// <param name="req">Request</param>  
  30.         /// <param name="inputKey"& gt;Request的键值</param>  
  31.         /// <param name="maxLen"& gt;最大长度</param>  
  32.         /// <returns>返回Request查询字符串</returns>  
  33.         public static string IsDigit(HttpRequest req, string inputKey, int maxLen)  
  34.         {  
  35.             string retVal = string.Empty;  
  36.             if(inputKey != null && inputKey != string.Empty)  
  37.             {  
  38.                 retVal = req.QueryString[inputKey];  
  39.                 if(null == retVal)  
  40.                     retVal = req.Form[inputKey];  
  41.                 if(null != retVal)  
  42.                 {  
  43.                     retVal = SqlText(retVal, maxLen);  
  44.                     if(!IsNumber(retVal))  
  45.                         retVal = string.Empty;  
  46.                 }  
  47.             }  
  48.             if(retVal == null)  
  49.                 retVal = string.Empty;  
  50.             return retVal;  
  51.         }  
  52.         /// <summary>  
  53.         /// 是否数字字符串  
  54.         /// </summary>  
  55.         /// <param name="inputData"& gt;输入字符串</param>  
  56.         /// <returns></returns>  
  57.         public static bool IsNumber(string inputData)  
  58.         {  
  59.             Match m = RegNumber.Match(inputData);  
  60.             return m.Success;  
  61.         }  
  62.         /// <summary>  
  63.         /// 是否数字字符串 可带正负号  
  64.         /// </summary>  
  65.         /// <param name="inputData"& gt;输入字符串</param>  
  66.         /// <returns></returns>  
  67.         public static bool IsNumberSign(string inputData)  
  68.         {  
  69.             Match m = RegNumberSign.Match(inputData);  
  70.             return m.Success;  
  71.         }  
  72.         /// <summary>  
  73.         /// 是否是浮点数  
  74.         /// </summary>  
  75.         /// <param name="inputData"& gt;输入字符串</param>  
  76.         /// <returns></returns>  
  77.         public static bool IsDecimal(string inputData)  
  78.         {  
  79.             Match m = RegDecimal.Match(inputData);  
  80.             return m.Success;  
  81.         }  
  82.         /// <summary>  
  83.         /// 是否是浮点数 可带正负号  
  84.         /// </summary>  
  85.         /// <param name="inputData"& gt;输入字符串</param>  
  86.         /// <returns></returns>  
  87.         public static bool IsDecimalSign(string inputData)  
  88.         {  
  89.             Match m = RegDecimalSign.Match(inputData);  
  90.             return m.Success;  
  91.         }         
  92.         #endregion  
  93.         #region 中文检测  
  94.         /// <summary>  
  95.         /// 检测是否有中文字符  
  96.         /// </summary>  
  97.         /// <param name="inputData"></param>  
  98.         /// <returns></returns>  
  99.         public static bool IsHasCHZN(string inputData)  
  100.         {  
  101.             Match m = RegCHZN.Match(inputData);  
  102.             return m.Success;  
  103.         }     
  104.         #endregion  
  105.         #region 邮件地址  
  106.         /// <summary>  
  107.         /// 是否是浮点数 可带正负号  
  108.         /// </summary>  
  109.         /// <param name="inputData"& gt;输入字符串</param>  
  110.         /// <returns></returns>  
  111.         public static bool IsEmail(string inputData)  
  112.         {  
  113.             Match m = RegEmail.Match(inputData);  
  114.             return m.Success;  
  115.         }         
  116.         #endregion  
  117.         #region 其他  
  118.         /// <summary>  
  119.         /// 检查字符串最大长度,返回指定长度的串  
  120.         /// </summary>  
  121.         /// <param name="sqlInput"& gt;输入字符串</param>  
  122.         /// <param name="maxLength">最大长度</param>  
  123.         /// <returns></returns>           
  124.         public static string SqlText(string sqlInput, int maxLength)  
  125.         {             
  126.             if(sqlInput != null && sqlInput != string.Empty)  
  127.             {  
  128.                 sqlInput = sqlInput.Trim();                           
  129.                 if(sqlInput.Length > maxLength)//按最大长度截取字符串  
  130.                     sqlInput = sqlInput.Substring(0, maxLength);  
  131.             }  
  132.             return sqlInput;  
  133.         }  
  134.         /// <summary>  
  135.         /// 字符串编码  
  136.         /// </summary>  
  137.         /// <param name="inputData"></param>  
  138.         /// <returns></returns>  
  139.         public static string HtmlEncode(string inputData)  
  140.         {  
  141.             return HttpUtility.HtmlEncode(inputData);  
  142.         }  
  143.         /// <summary>  
  144.         /// 设置Label显示Encode的字符串  
  145.         /// </summary>  
  146.         /// <param name="lbl"></param>  
  147.         /// <param name="txtInput"></param>  
  148.         public static void SetLabel(Label lbl, string txtInput)  
  149.         {  
  150.             lbl.Text = HtmlEncode(txtInput);  
  151.         }  
  152.         public static void SetLabel(Label lbl, object inputObj)  
  153.         {  
  154.             SetLabel(lbl, inputObj.ToString());  
  155.         }         
  156.         #endregion  
  157.     }  
  158. }  
posted on 2010-07-20 07:06  行万里路 责任 创新 执着  阅读(251)  评论(0编辑  收藏  举报