如何用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.gifContractedBlock.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.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.gif private   void  DisplayErrors()
ExpandedBlockStart.gifContractedBlock.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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值