国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

ASP.NET中數據有效性校驗的方法

2024-07-10 13:04:11
字體:
來源:轉載
供稿:網友


    作為一名程序員,一定要對自己編寫的程序的健壯性負責,因此數據的校驗無論在商業邏輯還是系統實現都是必不可少的部分。

    我這里總結了一種自認為比較不錯的asp.net(c#)的數據校驗方法,如大家探討。

    主要用regex的ismatch方法,在businessrule層進行校驗數據的有效性,并將校驗的方法作為businessrule層基類的一部分。

在webui層現實提示信息。

using system;
using system.data;
using system.text.regularexpressions;
namespace education.businessrules
{
 /// <summary>
 /// 商業規則層的基類
 /// </summary>
 public class bizobject
 {
  public const string regexp_is_valid_email = @"^/w+((-/w+)|(/./w+))*/@/w+((/.|-)/w+)*/./w+$";  //電子郵件校驗常量
  public const string regexp_is_valid_url  = @"^http://([/w-]+/.)+[/w-]+(/[/w- ./?%&=]*)?";    //網址校驗常量
  public const string regexp_is_valid_zip  = @"/d{6}";     //郵編校驗常量
  public const string regexp_is_valid_ssn  = @"/d{18}|/d{15}";    //身份證校驗常量
  public const string regexp_is_valid_int  = @"^/d{1,}$";     //整數校驗常量
  public const string regexp_is_valid_demical = @"^-?(0|/d+)(/./d+)?$";    //數值校驗常量 "
  //日期校驗常量
  public const string regexp_is_valid_date = @"^(?:(?:(?:(?:1[6-9]|[2-9]/d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(//|-|/.)(?:0?2/1(?:29))$)|(?:(?:1[6-9]|[2-9]/d)?/d{2})(//|-|/.)(?:(?:(?:0?[13578]|1[02])/2(?:31))|(?:(?:0?[1,3-9]|1[0-2])/2(29|30))|(?:(?:0?[1-9])|(?:1[0-2]))/2(?:0?[1-9]|1/d|2[0-8]))$";

  public bizobject(){}

  #region 校驗字段是否為空 或 字段長度超長 方法

  public string getfieldtoolongerror(string errorfield,int maxlen)
  { 
   return errorfield + "信息超長,請刪減至" + maxlen.tostring() + "個字符!" ;
  }

  public string getfieldnullerror(string errorfield)
  { 
   return errorfield + "是必填項,不允許為空!" ;
  }

  public bool isvalidfield(datarow row, string fieldname, int maxlen,string errorfield ,bool allownull)
  {
   int i = (short)(row[fieldname].tostring().trim().length);
           
   if ( i < 1 && (!allownull))
   {
    row.setcolumnerror(fieldname, getfieldnullerror(errorfield));               
    return false;
   }
   else if  (i > maxlen )
   {
    row.setcolumnerror(fieldname, getfieldtoolongerror(errorfield,maxlen));               
    return false;
   }           
   return true;
  }
  #endregion

  #region 校驗 電子郵件 類型字段格式 方法

  public string getemailfielderror(string errorfield)
  {
   return errorfield + "格式不正確([email protected])!" ;
  }
  public bool isvalidemail(datarow row, string fieldname,int maxlen ,string errorfield,bool allownull)
  {
   int  i = (short)(row[fieldname].tostring().trim().length);

   bool isvalid = isvalidfield(row,fieldname, maxlen , errorfield , allownull);
           
   if ( isvalid )
   {
    isvalid = (new regex(regexp_is_valid_email)).ismatch(row[fieldname].tostring());
               
    if ( (!isvalid) && (i > 0))
    {
     row.setcolumnerror(fieldname, getemailfielderror(errorfield));
     return false;
    }
   }           
   return true;
  }
  #endregion

  #region 校驗 郵編 類型字段格式 方法

  public string getzipfielderror(string errorfield)
  {
   return errorfield + "格式不正確(157032)!" ;
  }
  public bool isvalidzip(datarow row, string fieldname,int maxlen ,string errorfield,bool allownull)
  {
   int  i = (short)(row[fieldname].tostring().trim().length);

   bool isvalid = isvalidfield(row,fieldname, maxlen , errorfield , allownull);
           
   if ( isvalid )
   {
    isvalid = (new regex(regexp_is_valid_zip)).ismatch(row[fieldname].tostring());
               
    if ( (!isvalid) && (i > 0))
    {
     row.setcolumnerror(fieldname, getzipfielderror(errorfield));
     return false;
    }
   }           
   return true;
  }
  #endregion

  #region 校驗 身份證 類型字段格式 方法

  public string getssnfielderror(string errorfield)
  {
   return errorfield + "格式不正確(長度為15或18位)!" ;
  }
  public bool isvalidssn(datarow row, string fieldname,int maxlen ,string errorfield,bool allownull)
  {
   int  i = (short)(row[fieldname].tostring().trim().length);

   bool isvalid = isvalidfield(row,fieldname, maxlen , errorfield , allownull);
           
   if ( isvalid )
   {
    isvalid = (new regex(regexp_is_valid_ssn)).ismatch(row[fieldname].tostring());
               
    if ( (!isvalid) && (i > 0))
    {
     row.setcolumnerror(fieldname, getssnfielderror(errorfield));
     return false;
    }
   }           
   return true;
  }
  #endregion

  #region 校驗 網址 類型字段格式 方法

  public string geturlfielderror(string errorfield)
  {
   return errorfield + "格式不正確(http://www.abc.com)!" ;
  }
  public bool isvalidurl(datarow row, string fieldname,int maxlen ,string errorfield,bool allownull)
  {
   int  i = (short)(row[fieldname].tostring().trim().length);

   bool isvalid = isvalidfield(row,fieldname, maxlen , errorfield , allownull);
           
   if ( isvalid )
   {
    isvalid = (new regex(regexp_is_valid_url)).ismatch(row[fieldname].tostring());
               
    if ( (!isvalid) && (i > 0))
    {
     row.setcolumnerror(fieldname, geturlfielderror(errorfield));
     return false;
    }
   }           
   return true;
  }
  #endregion

  #region 校驗 日期 類型字段格式 方法

  public string getdatefielderror(string errorfield)
  {
   return errorfield + "日期格式不正確!" ;
  }
  public bool isvaliddate(datarow row, string fieldname,int maxlen ,string errorfield,bool allownull)
  {
   int  i = (short)(row[fieldname].tostring().trim().length);

   bool isvalid = isvalidfield(row,fieldname, maxlen , errorfield , allownull);
           
   if ( isvalid )
   {
    isvalid = (new regex(regexp_is_valid_date)).ismatch(row[fieldname].tostring());
               
    if ( (!isvalid) && (i > 0))
    {
     row.setcolumnerror(fieldname, getdatefielderror(errorfield));
     return false;
    }
   }           
   return true;
  }
  #endregion 

  #region 校驗 數值 類型字段格式 方法
  //這也是個判斷數值的辦法
  private bool isnumeric(string value)
  {
   try
   {
    int i = int.parse(value);
    return true;
   }
   catch
   { return false; }
  }

  public string getfieldnumbererror(string errorfield)
  { 
   return errorfield + "必須是數字(例如:90)!" ;
  }

  public bool isvalidnumber(datarow row, string fieldname,string errorfield,bool allownull)
  {
   int  i = (short)(row[fieldname].tostring().trim().length);
  
   bool isvalid = (new regex(regexp_is_valid_demical)).ismatch(row[fieldname].tostring());

   if ( i < 1 && (!allownull))
   {
    row.setcolumnerror(fieldname, getfieldnullerror(errorfield));               
    return false;
   }             
   else if ( (!isvalid) && (i > 0))
   {
    row.setcolumnerror(fieldname, getfieldnumbererror(errorfield));
    return false;
   }
   return true;
  }
  #endregion

 }
}

 

//在繼承了基類的businessrule中使用校驗的方法
  /// <summary>
  /// 使用上面的方法對數據進行有效性校驗
  /// </summary>
  /// <param name="row">數據行</param>
  /// <returns>通過--true 不通過--false</returns> 
  public bool validate(datarow row)
  {
   bool isvalid;           
   row.clearerrors();              
   isvalid   = isvalidfield(row, "name", 20 ,"姓名",false);     
   isvalid  &= isvalidzip(row, "zip", 6,"郵編",true);
   isvalid  &= isvalidnumber(row, "age","年齡",false);
   isvalid  &= isvalidemail(row,"email",50,"電子郵件" ,true); 
   return isvalid;
  }

 

//在webui中顯示錯誤提示信息
/// <summary>
/// 顯示提交數據返回的錯誤信息
/// </summary>
private void displayerrors()
{
 string  fielderrors="";
 string  tmpfielderrors="";

        datarow row = ds.tables[0].rows[0];

 foreach (datacolumn column in ds.tables[0].columns)
 {   
  tmpfielderrors = row.getcolumnerror(column.columnname.tostring());
  if (tmpfielderrors!="")
  {
   fielderrors += "<li>"  + tmpfielderrors + "<br>";
  }
 }
 //顯示錯誤信息
 this.lblerror.text = fielderrors;
}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 通渭县| 浦东新区| 中山市| 关岭| 泸水县| 北宁市| 嘉义市| 吉林市| 安塞县| 义乌市| 搜索| 济南市| 兴国县| 闽清县| 辽阳市| 广州市| 阜平县| 英超| 万盛区| 上栗县| 桓仁| 漳州市| 西林县| 宣武区| 垣曲县| 安陆市| 红原县| 墨脱县| 古蔺县| 武城县| 茂名市| 垦利县| 密山市| 迁西县| 盐源县| 灵寿县| 西藏| 龙岩市| 文水县| 颍上县| 贵德县|