shawl.qiu c# .net 服务端表单验证类 FormValidator 1.1

shawl.qiu c# .net 服务端表单验证类 FormValidator 1.1

说明:
俺是从夏历去年 12 月底开始学 .net 的, 主要为 c# 语言, 不过后来发现, .NET 用 vb/c#/js 其实都差不多, 主要嘛, 还是那个庞大的 framework...至目前为止还在啃 1.1...

话归正题, 自从俺对客户端 js 有了深刻了解之后, 写代码真的无法再忍受代码工式的重复又重复...一般都是多花一点时间, 把某个功能封装起来, 以便日后方便调用....这样以后需要相同功能的时候, 就不需要重复写代码了..

不过.NET 这么强大的一个架构, 对于 FORM 验证真是不敢恭维, 那些服务端验证控件...用起来那可不是一般的麻烦, 至少俺目前没发现什么简便的方法...

为什么不使用客户端脚本进行验证? 其实本人已经写了好几个成熟的客户端验证类, 不过本人还是认为服务端应该是最重要的....毕竟俺理解的脚本应用主要为改善用户操作...至于起什么安全作用, 那本人可不认为有什么作用...

因此本类也就诞生了....
但目前只是一个初步架构的定型, 以后将陆续增加功能

广告时间: 近期将陆续发布本人写的一些有关.NET 的相关代码...

下载:
http://files.myopera.com/btbtd/csharp/class/FormValidator%201.1.7z

目录:

1. 调用演示
1.1 默认错误提示
1.2 自定义错误提示

2. 类 FormValidator

shawl.qiu
2007-05-04
http://blog.csdn.net/btbtd

正文:

1. 调用演示
1.1 默认错误提示
  1.  void SubmitFunc(Object s, EventArgs e)
  2.  {
  3.   bool Debug = false;
  4.   
  5.   FormValidator fv = new FormValidator();
  6.    
  7.    fv.DebugLbl = DebugLbl;
  8.    fv.InfoLbl = InfoLbl;
  9.    
  10.    fv.MainCtl = Page.Controls[0];
  11.    
  12.    fv.Trim = true;
  13.    
  14.    fv.Debug = Debug;
  15.    
  16.    fv.IsNoEmpty = "gbname,gbeditpwd,gbtel,gbmobile, gbsubject, gbtext, gbemail";
  17.    fv.IsEmail = "gbemail, gbmsn";
  18.    fv.IsNumber = "gbqq";
  19.    
  20.    fv.Go();
  21.    
  22.    if(!fv.Valid)
  23.    {
  24.     fv = null;
  25.     return;
  26.    }
  27.    
  28.    fv = null;
  29.   
  30.   if(Debug)
  31.   {
  32.    Response.Write("<li/>SubmitFunc: Ok");
  33.   }
  34.  } // end void SubmitFunc

1.2 自定义错误提示
  1.  void SubmitFunc(Object s, EventArgs e)
  2.  {
  3.   bool Debug = true;
  4.   
  5.   FormValidator fv = new FormValidator();
  6.    
  7.    fv.DebugLbl = DebugLbl;
  8.    fv.InfoLbl = InfoLbl;
  9.    
  10.    fv.MainCtl = Page.Controls[0];
  11.    
  12.    fv.Trim = true;
  13.    
  14.    fv.Debug = false;
  15.    
  16.    fv.IsNoEmpty = "gbname:大名不能为空!,gbeditpwd:编辑密码不能为空!,gbtel,gbmobile, gbsubject, gbtext, gbemail";
  17.    fv.IsEmail = "gbemail:电子邮件格式错误!, gbmsn:msn 必须为电子邮件格式!";
  18.    fv.IsNumber = "gbqq:QQ号码必须为数字";
  19.    
  20.    fv.Go();
  21.    
  22.    if(!fv.Valid)
  23.    {
  24.     fv = null;
  25.     return;
  26.    }
  27.    
  28.    fv = null;
  29.   
  30.   if(Debug)
  31.   {
  32.    Response.Write("<li/>SubmitFunc: Ok");
  33.   }
  34.  } // end void SubmitFunc

2. 类 FormValidator
  1. /*-----------------------------------------------------------------------------------*/
  2.  * shawl.qiu c# .net FormValidator class v1.1
  3. /*-----------------------------------------------------------------------------------*/
  4. //---------------------------------------------------------------------begin class FormValidator
  5. public class FormValidator
  6. {
  7.  //-----------------------------------begin event
  8.  public FormValidator()
  9.  {
  10.  }
  11.  
  12.  ~FormValidator()
  13.  {
  14.  }
  15.  //-----------------------------------end event
  16.  
  17.  //-----------------------------------begin public constant
  18.  //-----------------------begin about
  19.  public const String auSubject = "shawl.qiu c# .net FormValidator";
  20.  public const String auVersion = "v1.1";
  21.  public const String au = "shawl.qiu";
  22.  public const String auEmail = "shawl.qiu@gmail.com";
  23.  public const String auBlog = "http://blog.csdn.net/btbtd";
  24.  public const String auCreateDate = "2007-5-4";
  25.  public const String auUpdate1 = "2007-5-4 20:38:36";
  26.  //-----------------------end about
  27.  //-----------------------------------end public constant
  28.  
  29.  //-----------------------------------begin private constant
  30.  //-----------------------------------end private constant
  31.  
  32.  //-----------------------------------begin public static method
  33.  //-----------------------------------end public static method
  34.  
  35.  //-----------------------------------begin private static method
  36.  //-----------------------------------end private static method
  37.  
  38.  //-----------------------------------begin public variable
  39.  public Label InfoLbl;
  40.  public Label DebugLbl;
  41.  
  42.  public Control MainCtl;
  43.  
  44.  public bool Valid = true;
  45.  public bool Trim = false;
  46.  
  47.  public bool Debug = false;
  48.  
  49.  public string IsNoEmpty = "";
  50.  public string IsEmail = "";
  51.  public string IsNumber = "";
  52.  
  53.  //-----------------------------------end public variable
  54.  
  55.  //-----------------------------------begin private variable
  56.  //-----------------------------------end private variable
  57.  
  58.  //-----------------------------------begin public method
  59.  public void Go()
  60.  {
  61.   if(MainCtl==null)
  62.   {
  63.    SetFalse("控件主域不能为 null!");
  64.    return;
  65.   }
  66.  
  67.   RegExp.ClearSpace(ref IsNoEmpty);
  68.   RegExp.ClearSpace(ref IsEmail);
  69.   
  70.   if(Debug)
  71.   {
  72.    HttpContext.Current.Response.Write("<li/>IsNoEmpty: "+IsNoEmpty);
  73.    HttpContext.Current.Response.Write("<li/>IsNoEmpty: "+IsEmail);
  74.   } // end if
  75.   
  76.   
  77.   foreach(Control Ctl in MainCtl.Controls)
  78.   {
  79.    if(Debug)
  80.    {
  81.     HttpContext.Current.Response.Write("<li/>"+Ctl.GetType().Name );
  82.    }
  83.    
  84.    Type Tp = Ctl.GetType();
  85.    
  86.    switch(Tp.Name)
  87.    {
  88.     case "TextBox":
  89.      
  90.      TextBox TempTbx = (TextBox)Ctl;
  91.      
  92.      if(Trim)
  93.      {
  94.       TempTbx.Text = TempTbx.Text.Trim();
  95.      }
  96.      
  97.      if(Debug)
  98.      {
  99.       HttpContext.Current.Response.Write("<li/>TempTbx: "+TempTbx.ID);
  100.       HttpContext.Current.Response.Write("<li/>TempTbx.Text: "+TempTbx.Text);
  101.      }
  102.      
  103.      if(IsNoEmpty!="")
  104.      {
  105.       if(RegExp.Test(IsNoEmpty, @"/b"+TempTbx.ID+@"/b"))
  106.       {
  107.        if(Debug)
  108.        {
  109.         HttpContext.Current.Response.Write("<li/>IsNoEmpty: "+TempTbx.ID);
  110.        }
  111.        
  112.        if(RegExp.IsEmpty(TempTbx.Text))
  113.        {
  114.         if(RegExp.Test(IsNoEmpty, @"/b"+TempTbx.ID+@"/b/:"))
  115.         {
  116.          string word = GetWord(IsNoEmpty, TempTbx.ID);
  117.          if(Debug)
  118.          {
  119.           HttpContext.Current.Response.Write(
  120.            word
  121.            );
  122.          }
  123.          SetFalse(word);
  124.         }
  125.         else
  126.         {
  127.          SetFalse("文本框 "+TempTbx.ID+" 值不能为空!");
  128.         }
  129.         SetFocus(InfoLbl, TempTbx.ID);
  130.         return;
  131.        } // enf if 2
  132.       } // end if 1
  133.      } // end if
  134.      
  135.      if(IsEmail!="")
  136.      {
  137.       if(RegExp.Test(IsEmail, @"/b"+TempTbx.ID+@"/b"))
  138.       {
  139.        if(Debug)
  140.        {
  141.         HttpContext.Current.Response.Write("<li/>IsEmail: "+TempTbx.ID);
  142.        } // end if 2
  143.        
  144.        if(!RegExp.IsEmail(TempTbx.Text)&!RegExp.IsEmpty(TempTbx.Text))
  145.        {
  146.         if(RegExp.Test(IsEmail, @"/b"+TempTbx.ID+@"/b/:"))
  147.         {
  148.          string word = GetWord(IsEmail, TempTbx.ID);
  149.          if(Debug)
  150.          {
  151.           HttpContext.Current.Response.Write(
  152.            word
  153.            );
  154.          }
  155.          SetFalse(word);
  156.         }
  157.         else
  158.         {
  159.         SetFalse("文本框 "+TempTbx.ID+" 值必须为电子邮件格式!");
  160.         }
  161.         SetFocus(InfoLbl, TempTbx.ID);
  162.         return;
  163.        } // enf if 2
  164.       } // end if 1
  165.      } // end if
  166.      
  167.      if(IsNumber!="")
  168.      {
  169.       if(RegExp.Test(IsNumber, @"/b"+TempTbx.ID+@"/b"))
  170.       {
  171.        if(Debug)
  172.        {
  173.         HttpContext.Current.Response.Write("<li/>IsNumber: "+TempTbx.ID);
  174.        } // end if 2
  175.        
  176.        if(RegExp.IsEmpty(TempTbx.Text))
  177.        {
  178.         TempTbx.Text = "0";
  179.        }
  180.      
  181.        if(!RegExp.IsNumber(TempTbx.Text)&!RegExp.IsEmpty(TempTbx.Text))
  182.        {
  183.         if(RegExp.Test(IsNumber, @"/b"+TempTbx.ID+@"/b/:"))
  184.         {
  185.          string word = GetWord(IsNumber, TempTbx.ID);
  186.          if(Debug)
  187.          {
  188.           HttpContext.Current.Response.Write(
  189.            word
  190.            );
  191.          }
  192.          SetFalse(word);
  193.         }
  194.         else
  195.         {
  196.          SetFalse("文本框 "+TempTbx.ID+" 值必须为数字!");
  197.         }
  198.          SetFocus(InfoLbl, TempTbx.ID);
  199.          return
  200.        } // end if 2
  201.        
  202.       } // end if 1
  203.      } // end if
  204.      
  205.      break;
  206.      
  207.    } // end switch
  208.    
  209.   } // end foreach
  210.  } // public void Go
  211.  //-----------------------------------end public method
  212.  
  213.  //-----------------------------------begin private method
  214.  //-----------------------------------end public static method
  215.  
  216.  private string GetWord(string ipt, string id)
  217.  {
  218.   return Regex.Replace(ipt,@"^.*/b"+id+@"/b/:([^,]+).*", "$1");
  219.  }
  220.  
  221.  private void SetFalse(string msg)
  222.  {
  223.   if(InfoLbl!=null)
  224.   {
  225.    Message(InfoLbl, msg);
  226.   }
  227.   Valid = false;
  228.  }
  229.  
  230.  private void SetFocus(Label InfoLbl, string id)
  231.  {
  232.   string scp = @"<script type='text/javascript'>"+
  233.    @"//<![CDATA["+"/r/n"+
  234.    @" var OnloadFunc;"+
  235.    @" try{OnloadFunc = onload}catch(e){}"+
  236.    @" onload = function()"+
  237.    @" {"+
  238.    @"  try{OnloadFunc();}catch(e){}"+
  239.    @"  var ctl = document.getElementById('"+id+"');"+
  240.    @"  if(ctl!=null)"+
  241.    @"  {"+
  242.    @"   ctl.focus();"+
  243.    @"  }"+
  244.    @" }"+
  245.    @"//]]>"+
  246.    @"</"+
  247.    @"script>";
  248.    
  249.    if(InfoLbl!=null)
  250.    {
  251.     InfoLbl.Text += scp;
  252.    }
  253.  } // end private void SetFocus
  254.  
  255.  private void Message(Label InfoLabel, string msg)
  256.  {
  257.   InfoLabel.Text +="<div style=/"display:table;width:100%;background-color:yellow!important;";
  258.    InfoLabel.Text +="color:black!important;text-align:center!important;margin:10px 0px;/">";
  259.   InfoLabel.Text += msg+"</div>";
  260.  }
  261.  
  262.  private void Message(string msg, Label InfoLabel)
  263.  {
  264.   Message(InfoLabel, msg);
  265.  }
  266.  
  267.  private void Message(Label InfoLabel, string msg, string charSet)
  268.  {
  269.   InfoLabel.Text+="<meta http-equiv=/"Content-Type/" content=/"text/html; charset="+
  270.   charSet+"/" />";
  271.   Message(InfoLabel, msg);
  272.  }

  273.  //-----------------------------------end private method
  274.  
  275.  //-----------------------------------begin public property
  276.  //-----------------------------------end public property
  277.   
  278.  //-----------------------------------begin private property
  279.  //-----------------------------------end private property
  280.  
  281.  /*-----------------------------------------------------------------------------------*/
  282.   * shawl.qiu class v RegExp 1.0
  283.  /*-----------------------------------------------------------------------------------*/
  284.  //---------------------------------------------------------------------begin class RegExp
  285.  public class RegExp
  286.  {
  287.   //-----------------------------------begin event
  288.   public RegExp()
  289.   {
  290.   }
  291.   
  292.   ~RegExp()
  293.   {
  294.   }
  295.   //-----------------------------------end event
  296.   
  297.   //-----------------------------------begin public constant
  298.   //-----------------------begin about
  299.   public const String auSubject = "shawl.qiu class v RegExp 1.0";
  300.   public const String auVersion = "v1.0";
  301.   public const String au = "shawl.qiu";
  302.   public const String auEmail = "shawl.qiu@gmail.com";
  303.   public const String auBlog = "http://blog.csdn.net/btbtd";
  304.   public const String auCreateDate = "2007-4-19";
  305.   //-----------------------end about
  306.   //-----------------------------------end public constant
  307.   
  308.   //-----------------------------------begin private constant
  309.   //-----------------------------------end private constant
  310.   
  311.   //-----------------------------------begin public static method
  312.   public static string Trim(string sForRemove)
  313.   {
  314.    return Regex.Replace(sForRemove, @"^/s+|/s+$", "");
  315.   }
  316.   
  317.   public static bool IsEmail(string sMail)
  318.   {
  319.    return Regex.IsMatch(sMail, @"@", RegexOptions.IgnoreCase);
  320.   }
  321.   
  322.   public static bool IsEmpty(string str)
  323.   { 
  324.    str = Regex.Replace(str, @"/s+", "");
  325.    if(str=="")return true;
  326.    return false;
  327.   }
  328.   
  329.   public static bool IsNumber(string str)
  330.   {
  331.    return Regex.IsMatch(str, @"^[0-9.]+$", RegexOptions.IgnoreCase);
  332.   }
  333.   
  334.   public static string GetBr(string ipt)
  335.   {
  336.    return Regex.Replace(ipt, @"(/r/n)", @"<br/>$1");
  337.   }
  338.   
  339.   public static string RemoveBr(string ipt)
  340.   {
  341.    return Regex.Replace(ipt, @"(<br/>|<br>|<br />)", @"", RegexOptions.IgnoreCase);
  342.   }
  343.   
  344.   public static bool Test(string ipt, string ptr, RegexOptions ro)
  345.   {
  346.    return Regex.IsMatch(@ipt, @ptr, ro);
  347.   }
  348.   
  349.   public static bool Test(string ipt, string ptr)
  350.   {
  351.    return Regex.IsMatch(@ipt, @ptr, RegexOptions.IgnoreCase);
  352.   }
  353.   
  354.   public static void ClearSpace(ref string ipt)
  355.   {
  356.    ipt = Regex.Replace(ipt, @"/s+", "");
  357.   }
  358.   //-----------------------------------end public static method
  359.   
  360.   //-----------------------------------begin private static method
  361.   //-----------------------------------end private static method
  362.   
  363.   //-----------------------------------begin public variable
  364.   //-----------------------------------end public variable
  365.   
  366.   //-----------------------------------begin private variable
  367.   //-----------------------------------end private variable
  368.   
  369.   //-----------------------------------begin public method
  370.   //-----------------------------------end public method
  371.   
  372.   //-----------------------------------begin private method
  373.   //-----------------------------------end private method
  374.   
  375.   //-----------------------------------begin public property
  376.   //-----------------------------------end public property
  377.    
  378.   //-----------------------------------begin private property
  379.   //-----------------------------------end private property
  380.  }
  381.  //---------------------------------------------------------------------end class RegExp

  382. }
  383. //---------------------------------------------------------------------end class FormValidator









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值