如何用asp.net实现校验功能!

简介:
原文:http://search.csdn.net/Expert/topic/2356/2356984.xml?temp=.249447


None.gif我这里总结了一种自认为比较不错的asp.net(C#)的数据校验方法,如大家探讨。
None.gif
None.gif    主要用Regex的IsMatch方法,在BusinessRule层进行校验数据的有效性,并将校验的方法作为BusinessRule层基类的一部分。
None.gif
None.gif在WebUI层现实提示信息。
None.gif
None.gif using System;
None.gif using System.Data;
None.gif using System.Text.RegularExpressions;
None.gif namespace Education.BusinessRules
ExpandedBlockStart.gif ContractedBlock.gif dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif /**//// <summary>
InBlock.gif 
/// 商业规则层的基类
ExpandedSubBlockEnd.gif 
/// </summary>

InBlock.gif public class BizObject
ExpandedSubBlockStart.gifContractedSubBlock.gif dot.gif{
InBlock.gif  public const String REGEXP_IS_VALID_EMAIL = @"^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$";  //电子邮件校验常量
InBlock.gif
  public const String REGEXP_IS_VALID_URL  = @"^http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?";    //网址校验常量
InBlock.gif
  public const String REGEXP_IS_VALID_ZIP  = @"\d{6}";     //邮编校验常量
InBlock.gif
  public const String REGEXP_IS_VALID_SSN  = @"\d{18}|\d{15}";    //身份证校验常量 
InBlock.gif
  public const String REGEXP_IS_VALID_INT  = @"^\d{1,}$";     //整数校验常量
InBlock.gif
  public const String REGEXP_IS_VALID_DEMICAL = @"^-?(0|\d+)(\.\d+)?$";    //数值校验常量 "
InBlock.gif  
//日期校验常量
InBlock.gif
  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]))$";
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  public BizObject()dot.gif{}
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  校验字段是否为空 或 字段长度超长 方法#region 校验字段是否为空 或 字段长度超长 方法
InBlock.gif
InBlock.gif  public string GetFieldTooLongError(string ErrorField,int maxlen)
ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{  
InBlock.gif   return ErrorField + "信息超长,请删减至" + maxlen.ToString() + "个字符!" ;
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  public string GetFieldNullError(string ErrorField)
ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{  
InBlock.gif   return ErrorField + "是必填项,不允许为空!" ;
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  public bool IsValidField(DataRow Row, String fieldName, int maxLen,string ErrorField ,bool AllowNull)
ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
InBlock.gif   int i = (short)(Row[fieldName].ToString().Trim().Length);
InBlock.gif            
InBlock.gif   if ( i < 1 && (!AllowNull))
ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{
InBlock.gif    Row.SetColumnError(fieldName, GetFieldNullError(ErrorField));                
InBlock.gif    return false;
ExpandedSubBlockEnd.gif   }

InBlock.gif   else if  (i > maxLen )
ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{
InBlock.gif    Row.SetColumnError(fieldName, GetFieldTooLongError(ErrorField,maxLen));                
InBlock.gif    return false;
ExpandedSubBlockEnd.gif   }
            
InBlock.gif   return true;
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif  #endregion

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  校验 电子邮件 类型字段格式 方法#region 校验 电子邮件 类型字段格式 方法
InBlock.gif
InBlock.gif  public string GetEmailFieldError(string ErrorField)
ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
InBlock.gif   return ErrorField + "格式不正确(a@b.c)!" ;
ExpandedSubBlockEnd.gif  }
 
InBlock.gif  public bool IsValidEmail(DataRow Row, String fieldName,int maxLen ,string ErrorField,bool AllowNull)
ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
InBlock.gif   int  i = (short)(Row[fieldName].ToString().Trim().Length);
InBlock.gif
InBlock.gif   bool isValid = IsValidField(Row,fieldName, maxLen , ErrorField , AllowNull);
InBlock.gif            
InBlock.gif   if ( isValid )
ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{
InBlock.gif    isValid = (new Regex(REGEXP_IS_VALID_EMAIL)).IsMatch(Row[fieldName].ToString());
InBlock.gif                
InBlock.gif    if ( (!isValid) && (i > 0))
ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
InBlock.gif     Row.SetColumnError(fieldName, GetEmailFieldError(ErrorField));
InBlock.gif     return false;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif   }
            
InBlock.gif   return true;
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif  #endregion

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  校验 邮编 类型字段格式 方法#region 校验 邮编 类型字段格式 方法
InBlock.gif
InBlock.gif  public string GetZipFieldError(string ErrorField)
ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
InBlock.gif   return ErrorField + "格式不正确(157032)!" ;
ExpandedSubBlockEnd.gif  }

InBlock.gif  public bool IsValidZip(DataRow Row, String fieldName,int maxLen ,string ErrorField,bool AllowNull)
ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
InBlock.gif   int  i = (short)(Row[fieldName].ToString().Trim().Length);
InBlock.gif
InBlock.gif   bool isValid = IsValidField(Row,fieldName, maxLen , ErrorField , AllowNull);
InBlock.gif            
InBlock.gif   if ( isValid )
ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{
InBlock.gif    isValid = (new Regex(REGEXP_IS_VALID_ZIP)).IsMatch(Row[fieldName].ToString());
InBlock.gif                
InBlock.gif    if ( (!isValid) && (i > 0))
ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
InBlock.gif     Row.SetColumnError(fieldName, GetZipFieldError(ErrorField));
InBlock.gif     return false;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif   }
            
InBlock.gif   return true;
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif  #endregion

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif校验 身份证 类型字段格式 方法#region 校验 身份证 类型字段格式 方法
InBlock.gif
InBlock.gif  public string GetSSNFieldError(string ErrorField)
ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
InBlock.gif   return ErrorField + "格式不正确(长度为15或18位)!" ;
ExpandedSubBlockEnd.gif  }

InBlock.gif  public bool IsValidSSN(DataRow Row, String fieldName,int maxLen ,string ErrorField,bool AllowNull)
ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
InBlock.gif   int  i = (short)(Row[fieldName].ToString().Trim().Length);
InBlock.gif
InBlock.gif   bool isValid = IsValidField(Row,fieldName, maxLen , ErrorField , AllowNull);
InBlock.gif            
InBlock.gif   if ( isValid )
ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{
InBlock.gif    isValid = (new Regex(REGEXP_IS_VALID_SSN)).IsMatch(Row[fieldName].ToString());
InBlock.gif                
InBlock.gif    if ( (!isValid) && (i > 0))
ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
InBlock.gif     Row.SetColumnError(fieldName, GetSSNFieldError(ErrorField));
InBlock.gif     return false;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif   }
            
InBlock.gif   return true;
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif  #endregion

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  校验 网址 类型字段格式 方法#region 校验 网址 类型字段格式 方法
InBlock.gif
InBlock.gif  public string GetUrlFieldError(string ErrorField)
ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
InBlock.gif   return ErrorField + "格式不正确(http://www.abc.com/)!" ;
ExpandedSubBlockEnd.gif  }

InBlock.gif  public bool IsValidUrl(DataRow Row, String fieldName,int maxLen ,string ErrorField,bool AllowNull)
ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
InBlock.gif   int  i = (short)(Row[fieldName].ToString().Trim().Length);
InBlock.gif
InBlock.gif   bool isValid = IsValidField(Row,fieldName, maxLen , ErrorField , AllowNull);
InBlock.gif            
InBlock.gif   if ( isValid )
ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{
InBlock.gif    isValid = (new Regex(REGEXP_IS_VALID_URL)).IsMatch(Row[fieldName].ToString());
InBlock.gif                
InBlock.gif    if ( (!isValid) && (i > 0))
ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
InBlock.gif     Row.SetColumnError(fieldName, GetUrlFieldError(ErrorField));
InBlock.gif     return false;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif   }
            
InBlock.gif   return true;
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif  #endregion

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  校验 日期 类型字段格式 方法#region 校验 日期 类型字段格式 方法
InBlock.gif
InBlock.gif  public string GetDateFieldError(string ErrorField)
ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
InBlock.gif   return ErrorField + "日期格式不正确!" ;
ExpandedSubBlockEnd.gif  }

InBlock.gif  public bool IsValidDate(DataRow Row, String fieldName,int maxLen ,string ErrorField,bool AllowNull)
ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
InBlock.gif   int  i = (short)(Row[fieldName].ToString().Trim().Length);
InBlock.gif
InBlock.gif   bool isValid = IsValidField(Row,fieldName, maxLen , ErrorField , AllowNull);
InBlock.gif            
InBlock.gif   if ( isValid )
ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{
InBlock.gif    isValid = (new Regex(REGEXP_IS_VALID_DATE)).IsMatch(Row[fieldName].ToString());
InBlock.gif                
InBlock.gif    if ( (!isValid) && (i > 0))
ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
InBlock.gif     Row.SetColumnError(fieldName, GetDateFieldError(ErrorField));
InBlock.gif     return false;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif   }
            
InBlock.gif   return true;
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif  #endregion
  
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  校验 数值 类型字段格式 方法#region 校验 数值 类型字段格式 方法
InBlock.gif  //这也是个判断数值的办法
InBlock.gif
  private bool IsNumeric(string Value)
ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
InBlock.gif   try
ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{
InBlock.gif    int i = int.Parse(Value);
InBlock.gif    return true;
ExpandedSubBlockEnd.gif   }

InBlock.gif   catch
ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gifreturn false; }
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  public string GetFieldNumberError(string ErrorField)
ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{  
InBlock.gif   return ErrorField + "必须是数字(例如:90)!" ;
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  public bool IsValidNumber(DataRow Row, String fieldName,string ErrorField,bool AllowNull)
ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
InBlock.gif   int  i = (short)(Row[fieldName].ToString().Trim().Length);
InBlock.gif   
InBlock.gif   bool isValid = (new Regex(REGEXP_IS_VALID_DEMICAL)).IsMatch(Row[fieldName].ToString());
InBlock.gif
InBlock.gif   if ( i < 1 && (!AllowNull))
ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{
InBlock.gif    Row.SetColumnError(fieldName, GetFieldNullError(ErrorField));                
InBlock.gif    return false;
ExpandedSubBlockEnd.gif   }
              
InBlock.gif   else if ( (!isValid) && (i > 0))
ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{
InBlock.gif    Row.SetColumnError(fieldName, GetFieldNumberError(ErrorField));
InBlock.gif    return false;
ExpandedSubBlockEnd.gif   }

InBlock.gif   return true;
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif  #endregion

InBlock.gif
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

None.gif
None.gif  
None.gif
None.gif // 在继承了基类的BusinessRule中使用校验的方法
ExpandedBlockStart.gifContractedBlock.gif
   /**/ /// <summary>
InBlock.gif  
/// 使用上面的方法对数据进行有效性校验
InBlock.gif  
/// </summary>
InBlock.gif  
/// <param name="Row">数据行</param>
ExpandedBlockEnd.gif  
/// <returns>通过--true 不通过--false</returns>  

None.gif   public  bool Validate(DataRow Row)
ExpandedBlockStart.gif ContractedBlock.gif   dot.gif {
InBlock.gif   bool isValid;            
InBlock.gif   Row.ClearErrors();               
InBlock.gif   isValid   = IsValidField(Row, "name", 20 ,"姓名",false);      
InBlock.gif   isValid  &= IsValidZip(Row, "zip", 6,"邮编",true); 
InBlock.gif   isValid  &= IsValidNumber(Row, "age","年龄",false);
InBlock.gif   isValid  &= IsValidEmail(Row,"email",50,"电子邮件" ,true);  
InBlock.gif   return isValid;
ExpandedBlockEnd.gif  }

None.gif
None.gif 
None.gif
None.gif // 在WebUI中显示错误提示信息
ExpandedBlockStart.gifContractedBlock.gif
/**/ /// <summary>
InBlock.gif
/// 显示提交数据返回的错误信息
ExpandedBlockEnd.gif
/// </summary>

None.gif private  void DisplayErrors()
ExpandedBlockStart.gif ContractedBlock.gif dot.gif {
InBlock.gif String  fieldErrors="";
InBlock.gif String  tmpfieldErrors="";
InBlock.gif
InBlock.gif        DataRow Row = ds.Tables[0].Rows[0];
InBlock.gif
InBlock.gif foreach (DataColumn Column in ds.Tables[0].Columns)
ExpandedSubBlockStart.gifContractedSubBlock.gif dot.gif{    
InBlock.gif  tmpfieldErrors = Row.GetColumnError(Column.ColumnName.ToString());
InBlock.gif  if (tmpfieldErrors!="")
ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
InBlock.gif   fieldErrors += "<li>"  + tmpfieldErrors + "<br>";
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif }

InBlock.gif //显示错误信息
InBlock.gif
 this.lblError.Text = fieldErrors;
ExpandedBlockEnd.gif}

None.gif

本文转自浪子博客园博客,原文链接:http://www.cnblogs.com/walkingboy/archive/2005/07/04/185827.html,如需转载请自行联系原作者
目录
相关文章
|
14天前
|
存储 文字识别 C#
.NET开源免费、功能强大的 Windows 截图录屏神器
今天大姚给大家分享一款.NET开源免费(基于GPL3.0开源协议)、功能强大、简洁灵活的 Windows 截图、录屏、Gif动图制作神器:ShareX。
|
关系型数据库 MySQL
【Mysql】服务没有响应控制功能。 请键入 NET HELPMSG 2186 以获得更多的帮助。
解决方法: 1. 下载dll文件 https://www.aliyundrive.com/s/oV6GgghtPkN 2.将文件放置在mysql bin文件夹下 3. 重新启动Mysql,发现启动成功了!🚀
727 0
|
1月前
|
Windows
windows server 2019 安装NET Framework 3.5失败,提示:“安装一个或多个角色、角色服务或功能失败” 解决方案
windows server 2019 安装NET Framework 3.5失败,提示:“安装一个或多个角色、角色服务或功能失败” 解决方案
100 0
|
2月前
|
C# Windows
.NET开源的一个小而快并且功能强大的 Windows 动态桌面软件
.NET开源的一个小而快并且功能强大的 Windows 动态桌面软件
|
7月前
|
Apache
基于commons-net实现ftp创建文件夹、上传、下载功能.
基于commons-net实现ftp创建文件夹、上传、下载功能.
106 0
|
9月前
|
移动开发 监控 网络协议
基于Socket通讯(C#)和WebSocket协议(net)编写的两种聊天功能(文末附源码下载地址)
基于Socket通讯(C#)和WebSocket协议(net)编写的两种聊天功能(文末附源码下载地址)
|
5月前
|
开发框架 前端开发 .NET
用ajax和asp.net实现智能搜索功能
用ajax和asp.net实现智能搜索功能
43 0
|
9月前
|
SQL 安全 前端开发
.NET开源免费功能最全的商城项目
.NET开源免费功能最全的商城项目
|
9月前
|
开发框架 前端开发 安全
ASP.NET Core MVC 从入门到精通之Html辅助标签补充及模型校验基础
ASP.NET Core MVC 从入门到精通之Html辅助标签补充及模型校验基础
93 0
|
9月前
|
开发框架 前端开发 JavaScript
WPF+ASP.NET SignalR实现简易在线聊天功能
WPF+ASP.NET SignalR实现简易在线聊天功能
129 0