动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
  
   .Net → 阅读文章

 ASP.NET中数据有效性校验的方法

作者来源: 
阅读 1583 人次 , 2006-3-29 4:34:00 


  作为一名程序员,一定要对自己编写的程序的健壮性负责,因此数据的校验无论在商业逻辑还是系统实现都是必不可少的部分。

  我这里总结了一种自认为比较不错的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 + "格式不正确(a@b.c)!" ;
  }
  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;
}

 本文TagsC#  
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:自定义Http处理及应用之HttpHandler篇
· 下一篇:数据库连接字在Web.config里的用法
· C#中方法参数的四种类型
· Application事件的执行顺序
· ListBox Web 控件
· Allair JRUN 非法读取 WEB-INF 漏洞
· ASP.NET图象处理详解


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077