正则表达式都在程序里,呵呵……

 

using System; 
 using System.Data; 
 using System.Windows.Forms; 
  
  
 namespace HEWin.Sys 
 { 
     /** <summary> 
     /// 功能:验证DataTable、DataRow和DataForm数据的合法性 
     /// 原理:使用DataColoum的扩展属性和窗口控件的Tag属性,存放数据的规范,调用本类来验证输入数据的合法性. 
     /// </summary> 
     public class sysValidate: IDisposable 
     { 
         private HEWin.Sys.sysErrors _Errors=new sysErrors(); 
         private System.Windows.Forms.Control _FocusControl; 
         static  private string UnString=@"\\'"; 
         static  private System.Text.RegularExpressions.Regex  _CHAR=new System.Text.RegularExpressions.Regex("["+UnString+"]"); 
          
         public     void  Dispose() 
         { 
          _Errors.Dispose(); 
  
         } 
         public      sysValidate()    
         { 
         } 
         /** <summary> 
         /// 功能:判断一个窗体上所有控件的数据合法性 
         /// 原理:根据控件的Tag属性存放的数据规范来验证数据的合法性, 
         ///      将错误返回到错误对象集合里. 
         /// </summary> 
         /// <param name="frm">窗口的引用</param> 
         /// <returns>错误集合对象</returns> 
         public Sys.sysErrors validateForm(System.Windows.Forms.Form frm) 
         { 
             this._FocusControl=null; 
             if(_Errors!=null) _Errors.Dispose(); 
             _Errors    =new HEWin.Sys.sysErrors(); 
             foreach(System.Windows.Forms.Control ctl in frm.Controls) 
                 _Errors.MergeErrors(validateControl(ctl)); 
             if(this._FocusControl!=null) 
                 this._FocusControl.Focus(); 
             return _Errors; 
         } 
  
         /** <summary> 
         /// 功能:判断一个窗体上可见控件的数据合法性 
         /// 原理:根据控件的Tag属性存放的数据规范来验证数据的合法性, 
         ///      将错误返回到错误对象集合里. 
         /// </summary> 
         /// <param name="frm">窗口的引用</param> 
         /// <param name="Visible">true:表示对可见控件进行判断</param> 
         /// <returns>错误集合对象</returns> 
         public Sys.sysErrors validateForm(System.Windows.Forms.Form frm,bool Enabled) 
         { 
             this._FocusControl=null; 
             if(_Errors!=null) _Errors.Dispose(); 
             _Errors    =new HEWin.Sys.sysErrors(); 
             foreach(System.Windows.Forms.Control ctl in frm.Controls) 
                 _Errors.MergeErrors(validateControl(ctl,Enabled)); 
             if(this._FocusControl!=null) 
                 this._FocusControl.Focus(); 
             return _Errors; 
         } 
         /** <summary> 
         /// 功能:判断一个控件的数据合法性 
         /// 原理:根据控件的Tag属性存放的数据规范来验证数据的合法性, 
         ///      将错误返回到错误对象集合里. 
         /// </summary> 
         /// <param name="ctl">控件的引用</param> 
         /// <returns>错误集合对象</returns> 
         public Sys.sysErrors validateControl(System.Windows.Forms.Control ctl) 
         { 
             HEWin.Sys.sysErrors errs    = new HEWin.Sys.sysErrors(); 
              
             switch (ctl.GetType().ToString()) 
             { 
                 case "System.Windows.Forms.GroupBox" : 
                     foreach(System.Windows.Forms.Control ctlInner in ctl.Controls) 
                         errs.MergeErrors(validateControl(ctlInner));        //递归 
                     break; 
                 case "System.Windows.Forms.Panel" : 
                     foreach(System.Windows.Forms.Control ctlInner in ctl.Controls) 
                         errs.MergeErrors(validateControl(ctlInner));        //递归 
                     break; 
                 default : 
                     Sys.sysError err = validate(ctl); 
                     if (err != null) 
                     { 
                         if (this._FocusControl==null && ctl.Visible) 
                             this._FocusControl=ctl; 
                         if(this._FocusControl!=null && ctl.TabIndex<this._FocusControl.TabIndex && ctl.Visible) 
                             this._FocusControl=ctl; 
                         errs.AddError(err); 
                     } 
                     break; 
             } 
             return errs; 
         } 
      
         /** <summary> 
         /// 功能:判断一个可见控件的数据合法性 
         /// 原理:根据控件的Tag属性存放的数据规范来验证数据的合法性, 
         ///      将错误返回到错误对象集合里. 
         /// </summary> 
         /// <param name="ctl">控件的引用</param> 
         /// <param name="Visible">true:表示对可见控件进行判断</param> 
         /// <returns>错误集合对象</returns> 
         public Sys.sysErrors validateControl(System.Windows.Forms.Control ctl,bool Enabled) 
         { 
  
             HEWin.Sys.sysErrors errs    = new HEWin.Sys.sysErrors(); 
              
             switch (ctl.GetType().ToString()) 
             { 
                 case "System.Windows.Forms.GroupBox" : 
                     foreach(System.Windows.Forms.Control ctlInner in ctl.Controls) 
                         errs.MergeErrors(validateControl(ctlInner,Enabled));        //递归 
                     break; 
                 case "System.Windows.Forms.Panel" : 
                     foreach(System.Windows.Forms.Control ctlInner in ctl.Controls) 
                         errs.MergeErrors(validateControl(ctlInner,Enabled));        //递归 
                     break; 
                 default : 
                     if (ctl.Visible!=Enabled)   
                         return errs; 
                      
                     if(ctl is System.Windows.Forms.TextBox&&( ((TextBox)ctl).ReadOnly==Enabled)) 
                         return errs; 
  
                     Sys.sysError err = validate(ctl); 
                     if (err != null) 
                     { 
                         if (this._FocusControl==null && ctl.Visible) 
                             this._FocusControl=ctl; 
                         if(this._FocusControl!=null && ctl.TabIndex<this._FocusControl.TabIndex && ctl.Visible) 
                             this._FocusControl=ctl; 
                         errs.AddError(err); 
                     } 
                     break; 
             } 
             return errs; 
         } 
      
         /** <summary> 
         /// 功能:判断一个控件数据的合法性 
         /// 原理:根据控件的Tag属性存放的数据规范来验证数据的合法性,所有验证控件的函数都是调用此函数来执行验证的. 
         /// </summary> 
         /// <param name="ctl">控件的引用</param> 
         /// <returns>返回错误集合对象</returns> 
         private Sys.sysError validate(System.Windows.Forms.Control ctl) 
         { 
              
              
             if (ctl.Tag != null && ctl.Tag.ToString() != "0;") 
             { 
                 string strTag = ctl.Tag.ToString(); 
                 string strText = ctl.Text; 
  
                 if (strTag.IndexOf(";") == -1) 
                     return null; 
  
                 string strVid = ""; 
                 System.Text.RegularExpressions.Regex regex; 
                 if (strTag.Length >= 1) 
                     strVid = strTag.Substring(0,1); 
                 else 
                     return null; 
                 string strVname = ""; 
                 string strVmaxmin = ""; 
                 string strFill = "0"; 
  
                 Sys.sysError _Error = new Sys.sysError(); 
  
                 switch (strVid) 
                 { 
                     case "1" : 
                         try 
                         { 
                             regex = new System.Text.RegularExpressions.Regex 
                                 (@"^\d+;(?<vname>[\u4E00-\u9FA0]+);(?<vfill>\d+);(?<vmaxmin>\d+,\d+);(?<vfraction>\d+);");    //匹配数字验证的正则表达式 
  
                             strVname = regex.Match(strTag).Result("${vname}"); 
                             strFill = regex.Match(strTag).Result("${vfill}"); 
                             strVmaxmin = regex.Match(strTag).Result("${vmaxmin}"); 
                             int intFraction = Convert.ToInt32(regex.Match(strTag).Result("${vfraction}")); 
  
                             if (strFill == "1") 
                             { 
                                 if (strText == "")                                            //是否必填 
                                 { 
                                     _Error = new HEWin.Sys.sysError(-1,strVname,1001,"不能为空!");                // D(1) = B(00000001) 
                                     return _Error; 
                                 } 
                             } 
                             else 
                             { 
                                 if (strText == "") 
                                     return null; 
                             } 
  
  
                             regex = new System.Text.RegularExpressions.Regex(@"^\d*\.?\d+$");        //是否有非法字符(此处必须全部为数字) 
                             if (strText != ""&&(!regex.IsMatch(strText)||strText.Substring(0,1)==".")) 
                             { 
                                 _Error = new HEWin.Sys.sysError(-1,strVname,1002,"必须为数字!");                                        // D(16) = B(00010000) 
                                 return _Error; 
                             } 
  
                             try 
                             { 
                                 System.Decimal test=System.Convert.ToDecimal(strText); 
                             } 
                             catch 
                             { 
                                 _Error = new HEWin.Sys.sysError(-1,strVname,1002,"必须为数字!");                                        // D(16) = B(00010000) 
                                 return _Error    ; 
                             } 
                          
  
  
                             regex = new System.Text.RegularExpressions.Regex(@"^(?<vmin>\d+),(?<vmax>\d+)", 
                                 System.Text.RegularExpressions.RegexOptions.Compiled); 
  
  
                             //要判断非常大的数据吗?? 
  
                             long lngMax = 0; 
                             long lngMin = 0; 
                          
                             try 
                             { 
                                 lngMax = Convert.ToInt64(regex.Match(strVmaxmin).Result("${vmax}")); 
                                 lngMin = Convert.ToInt64(regex.Match(strVmaxmin).Result("${vmin}")); 
  
                                 if (strText == "0") 
                                 { 
                                     _Error = new sysError(-1,strVname,1003,"不能为0!"); 
                                     return _Error;                        //非0判断 
                                 } 
  
                                 if (System.Math.Ceiling(Convert.ToDouble(strText)) > lngMax)        //最大值判断 
                                     if (System.Math.Floor(Convert.ToDouble(strText)) != lngMax) 
                                     { 
                                         _Error = new sysError(-1,strVname,1004,"最大值超出范围!");                                                // D(2) = B(00000010) 
                                         return _Error; 
                                     } 
  
                                 if (System.Math.Ceiling(Convert.ToDouble(strText)) < lngMin)                            //最小值判断 
                                 { 
                                     _Error = new sysError(-1,strVname,1005,"最小值超出范围!");                                                // D(4) = B(00000100) 
                                     return _Error; 
                                 } 
                             } 
                             catch 
                             { 
                                 _Error = new sysError(-1,strVname,1006,"所填数据过大!");                                            //所填数据过大 
                                 return _Error; 
                             } 
  
                             if (strText.IndexOf(".") != -1) 
                             { 
                                 regex = new System.Text.RegularExpressions.Regex(@"^\d+\.(?<vfraction>\d+$)"); 
                                 if (regex.Match(strText).Result("${vfraction}").Length > intFraction)    //小数位数判断 
                                 { 
                                     _Error = new sysError(-1,strVname,1007,"小数位数过长!");                                                        // D(8) = B(00001000)                         
                                     return _Error; 
                                 } 
                                  
                             } 
                         } 
                         catch 
                         { 
                             _Error = new sysError(-1,strVname,1499,"判断条件出错!");                //判断条件出错 
                             return _Error; 
                         } 
                         break; 
                     case "2" : 
                         try 
                         { 
                             //检查控件的Tag属性设置是否正确 
                             regex = new System.Text.RegularExpressions.Regex 
                                 (@"^\d+;(?<vname>[\u4E00-\u9FA0]+);(?<vfill>\d+);(?<vmaxmin>\d+,\d+);"); 
                          
                             strVname = regex.Match(strTag).Result("${vname}"); 
                             strFill = regex.Match(strTag).Result("${vfill}"); 
                             strVmaxmin = regex.Match(strTag).Result("${vmaxmin}"); 
  
                             //判断是否必填 
                             if (strFill == "1") 
                             { 
                                 if (strText == "") 
                                 { 
                                     _Error = new sysError(-1,strVname,1001,"不能为空!"); 
                                     return _Error; 
                                 } 
                             } 
  
                             //判断是否有非法字符 
                             if (_CHAR.IsMatch(strText) && strText != "")     
                             { 
                                 _Error = new sysError(-1,strVname,1008,"存在“"+UnString+"“等非法字符!"); 
                                 return _Error; 
                             } 
  
  
                             if (_CHAR.IsMatch(strText) && strText != "")     
                             { 
                                 _Error = new sysError(-1,strVname,1008,"存在“"+UnString+"”等非法字符!"); 
                                 return _Error; 
                             } 
  
  
                             //判断字符最大最小长度 
                             //HEWin.Sys.sysFunction.MsgWarning(strVmaxmin); 
                             regex = new System.Text.RegularExpressions.Regex(@"^(?<vmin>\d+),(?<vmax>\d+)"); 
                             int intMax = Convert.ToInt32(regex.Match(strVmaxmin).Result("${vmax}")); 
                             int intMin = Convert.ToInt32(regex.Match(strVmaxmin).Result("${vmin}")); 
                             //最大字符长度 
                             if (strText.Length > intMax) 
                             { 
                                 _Error = new sysError(-1,strVname,1009,"所填字符太长!"); 
                                 return _Error; 
                             } 
                                 //最小长度 
                             else if (strText.Length < intMin) 
                             { 
                                 _Error = new sysError(-1,strVname,1010,"所填字符太短!"); 
                                 return _Error;                                                                 
                             } 
  
                         } 
                         catch 
                         { 
                             _Error = new sysError(-1,strVname,1499,"判断条件出错!");                //判断条件出错 
                             return _Error; 
                         } 
                                          
                         break; 
  
                     case "3" : 
                         try 
                         { 
                             regex = new System.Text.RegularExpressions.Regex 
                                 (@"^\d+;(?<vname>[\u4E00-\u9FA0]+);(?<vfill>\d+);(?<vmaxmin>\d{4}\-\d{1,2}\-\d{1,2},\d{4}\-\d{1,2}\-\d{1,2});", 
                                 System.Text.RegularExpressions.RegexOptions.Compiled); 
                             //DateTimePicker控件 
                             if (ctl.GetType().ToString() == "System.Windows.Forms.DateTimePicker") 
                                 strText = ((System.Windows.Forms.DateTimePicker)ctl).Value.ToString("yyyy-MM-dd"); 
  
                             strVname = regex.Match(strTag).Result("${vname}"); 
                             strFill = regex.Match(strTag).Result("${vfill}"); 
                             strVmaxmin = regex.Match(strTag).Result("${vmaxmin}"); 
  
  
                             //是否必填 
                             if (strFill == "1") 
                             { 
                                 if (strText == "") 
                                 { 
                                     _Error = new sysError(-1,strVname,1001,"不能为空!");                //判断条件出错 
                                     return _Error; 
                                 } 
                             } 
  
                             //最大最小日期 
                             regex = new System.Text.RegularExpressions.Regex(@"^(?<vmin>\d{4}\-\d{1,2}\-\d{1,2}),(?<vmax>\d{4}\-\d{1,2}\-\d{1,2})"); 
                             //最大日期 
  
                             try 
                             { 
                                 if (Convert.ToDateTime(strText) > Convert.ToDateTime(regex.Match(strVmaxmin).Result("${vmax}"))) 
                                 { 
                                     _Error = new sysError(-1,strVname,1011,"所填日期太大!");                 
                                     return _Error; 
                                 } 
                                     //最小日期 
                                 else if (Convert.ToDateTime(strText) < Convert.ToDateTime(regex.Match(strVmaxmin).Result("${vmin}"))) 
                                 { 
                                     _Error = new sysError(-1,strVname,1011,"所填日期太小!");     
                                     return _Error; 
                                 } 
                             } 
                             catch 
                             { 
                                 _Error = new sysError(-1,strVname,1012,"不是类似“yyyy-MM-dd“格式的日期!"); 
                                 return _Error; 
                             } 
  
                         } 
                         catch 
                         { 
                             _Error = new sysError(-1,strVname,1499,"判断条件出错!");                //判断条件出错 
                             return _Error; 
                         } 
  
                         break; 
  
                     default : 
                         _Error = new sysError(-1,,1498,"Tag判断条件无法解析!"); 
                         return _Error; 
                 }             
             } 
              
             return null; 
         } 
     } 
 }