开发者社区> 长征6号> 正文

如何用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.gifusing System;
None.gifusing System.Data;
None.gifusing System.Text.RegularExpressions;
None.gifnamespace Education.BusinessRules
ExpandedBlockStart.gifContractedBlock.gifdot.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.gifContractedBlock.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.gifprivate void DisplayErrors()
ExpandedBlockStart.gifContractedBlock.gifdot.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,如需转载请自行联系原作者

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
学习ASP.NET Core Razor 编程系列十二——在页面中增加校验
原文:学习ASP.NET Core Razor 编程系列十二——在页面中增加校验 学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.
1338 0
Asp.net mvc 2 in action 笔记- 4 自动代码生成、校验Validation
自动代码生成 T4 (Text Template Transformation Toolkit) is a little-known feature of Visual Studio. It’s a code-generation toolkit, and its templates allow ...
801 0
跟我一起学习ASP.NET 4.5 MVC4.0(六)
原文http://www.cnblogs.com/xdotnet/archive/2012/07/21/aspnet40_webpage20.html 这一系列文章跨度有点大,由于最近忙于其他事情,没有更新,今天重新安装了下Win8系 统,VS2012和SQLServer 2012,顺便抽空继续一篇。
1007 0
跟我一起学习ASP.NET 4.5 MVC4.0(五)
原文http://www.cnblogs.com/xdotnet/archive/2012/03/29/aspnet_mvc4_html_control_checkboxlist.html 前面几篇文章介绍了一下ASP.NET MVC中的一些基础,今天我们一起来学习一下在ASP.NET MVC中控件的封装。
941 0
+关注
长征6号
文章
问答
视频
文章排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载
相关实验场景
更多