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 默认错误提示
- void SubmitFunc(Object s, EventArgs e)
- {
- bool Debug = false;
- FormValidator fv = new FormValidator();
- fv.DebugLbl = DebugLbl;
- fv.InfoLbl = InfoLbl;
- fv.MainCtl = Page.Controls[0];
- fv.Trim = true;
- fv.Debug = Debug;
- fv.IsNoEmpty = "gbname,gbeditpwd,gbtel,gbmobile, gbsubject, gbtext, gbemail";
- fv.IsEmail = "gbemail, gbmsn";
- fv.IsNumber = "gbqq";
- fv.Go();
- if(!fv.Valid)
- {
- fv = null;
- return;
- }
- fv = null;
- if(Debug)
- {
- Response.Write("<li/>SubmitFunc: Ok");
- }
- } // end void SubmitFunc
- void SubmitFunc(Object s, EventArgs e)
- {
- bool Debug = true;
- FormValidator fv = new FormValidator();
- fv.DebugLbl = DebugLbl;
- fv.InfoLbl = InfoLbl;
- fv.MainCtl = Page.Controls[0];
- fv.Trim = true;
- fv.Debug = false;
- fv.IsNoEmpty = "gbname:大名不能为空!,gbeditpwd:编辑密码不能为空!,gbtel,gbmobile, gbsubject, gbtext, gbemail";
- fv.IsEmail = "gbemail:电子邮件格式错误!, gbmsn:msn 必须为电子邮件格式!";
- fv.IsNumber = "gbqq:QQ号码必须为数字";
- fv.Go();
- if(!fv.Valid)
- {
- fv = null;
- return;
- }
- fv = null;
- if(Debug)
- {
- Response.Write("<li/>SubmitFunc: Ok");
- }
- } // end void SubmitFunc
2. 类 FormValidator
- /*-----------------------------------------------------------------------------------*/
- * shawl.qiu c# .net FormValidator class v1.1
- /*-----------------------------------------------------------------------------------*/
- //---------------------------------------------------------------------begin class FormValidator
- public class FormValidator
- {
- //-----------------------------------begin event
- public FormValidator()
- {
- }
- ~FormValidator()
- {
- }
- //-----------------------------------end event
- //-----------------------------------begin public constant
- //-----------------------begin about
- public const String auSubject = "shawl.qiu c# .net FormValidator";
- public const String auVersion = "v1.1";
- public const String au = "shawl.qiu";
- public const String auEmail = "shawl.qiu@gmail.com";
- public const String auBlog = "http://blog.csdn.net/btbtd";
- public const String auCreateDate = "2007-5-4";
- public const String auUpdate1 = "2007-5-4 20:38:36";
- //-----------------------end about
- //-----------------------------------end public constant
- //-----------------------------------begin private constant
- //-----------------------------------end private constant
- //-----------------------------------begin public static method
- //-----------------------------------end public static method
- //-----------------------------------begin private static method
- //-----------------------------------end private static method
- //-----------------------------------begin public variable
- public Label InfoLbl;
- public Label DebugLbl;
- public Control MainCtl;
- public bool Valid = true;
- public bool Trim = false;
- public bool Debug = false;
- public string IsNoEmpty = "";
- public string IsEmail = "";
- public string IsNumber = "";
- //-----------------------------------end public variable
- //-----------------------------------begin private variable
- //-----------------------------------end private variable
- //-----------------------------------begin public method
- public void Go()
- {
- if(MainCtl==null)
- {
- SetFalse("控件主域不能为 null!");
- return;
- }
- RegExp.ClearSpace(ref IsNoEmpty);
- RegExp.ClearSpace(ref IsEmail);
- if(Debug)
- {
- HttpContext.Current.Response.Write("<li/>IsNoEmpty: "+IsNoEmpty);
- HttpContext.Current.Response.Write("<li/>IsNoEmpty: "+IsEmail);
- } // end if
- foreach(Control Ctl in MainCtl.Controls)
- {
- if(Debug)
- {
- HttpContext.Current.Response.Write("<li/>"+Ctl.GetType().Name );
- }
- Type Tp = Ctl.GetType();
- switch(Tp.Name)
- {
- case "TextBox":
- TextBox TempTbx = (TextBox)Ctl;
- if(Trim)
- {
- TempTbx.Text = TempTbx.Text.Trim();
- }
- if(Debug)
- {
- HttpContext.Current.Response.Write("<li/>TempTbx: "+TempTbx.ID);
- HttpContext.Current.Response.Write("<li/>TempTbx.Text: "+TempTbx.Text);
- }
- if(IsNoEmpty!="")
- {
- if(RegExp.Test(IsNoEmpty, @"/b"+TempTbx.ID+@"/b"))
- {
- if(Debug)
- {
- HttpContext.Current.Response.Write("<li/>IsNoEmpty: "+TempTbx.ID);
- }
- if(RegExp.IsEmpty(TempTbx.Text))
- {
- if(RegExp.Test(IsNoEmpty, @"/b"+TempTbx.ID+@"/b/:"))
- {
- string word = GetWord(IsNoEmpty, TempTbx.ID);
- if(Debug)
- {
- HttpContext.Current.Response.Write(
- word
- );
- }
- SetFalse(word);
- }
- else
- {
- SetFalse("文本框 "+TempTbx.ID+" 值不能为空!");
- }
- SetFocus(InfoLbl, TempTbx.ID);
- return;
- } // enf if 2
- } // end if 1
- } // end if
- if(IsEmail!="")
- {
- if(RegExp.Test(IsEmail, @"/b"+TempTbx.ID+@"/b"))
- {
- if(Debug)
- {
- HttpContext.Current.Response.Write("<li/>IsEmail: "+TempTbx.ID);
- } // end if 2
- if(!RegExp.IsEmail(TempTbx.Text)&!RegExp.IsEmpty(TempTbx.Text))
- {
- if(RegExp.Test(IsEmail, @"/b"+TempTbx.ID+@"/b/:"))
- {
- string word = GetWord(IsEmail, TempTbx.ID);
- if(Debug)
- {
- HttpContext.Current.Response.Write(
- word
- );
- }
- SetFalse(word);
- }
- else
- {
- SetFalse("文本框 "+TempTbx.ID+" 值必须为电子邮件格式!");
- }
- SetFocus(InfoLbl, TempTbx.ID);
- return;
- } // enf if 2
- } // end if 1
- } // end if
- if(IsNumber!="")
- {
- if(RegExp.Test(IsNumber, @"/b"+TempTbx.ID+@"/b"))
- {
- if(Debug)
- {
- HttpContext.Current.Response.Write("<li/>IsNumber: "+TempTbx.ID);
- } // end if 2
- if(RegExp.IsEmpty(TempTbx.Text))
- {
- TempTbx.Text = "0";
- }
- if(!RegExp.IsNumber(TempTbx.Text)&!RegExp.IsEmpty(TempTbx.Text))
- {
- if(RegExp.Test(IsNumber, @"/b"+TempTbx.ID+@"/b/:"))
- {
- string word = GetWord(IsNumber, TempTbx.ID);
- if(Debug)
- {
- HttpContext.Current.Response.Write(
- word
- );
- }
- SetFalse(word);
- }
- else
- {
- SetFalse("文本框 "+TempTbx.ID+" 值必须为数字!");
- }
- SetFocus(InfoLbl, TempTbx.ID);
- return;
- } // end if 2
- } // end if 1
- } // end if
- break;
- } // end switch
- } // end foreach
- } // public void Go
- //-----------------------------------end public method
- //-----------------------------------begin private method
- //-----------------------------------end public static method
- private string GetWord(string ipt, string id)
- {
- return Regex.Replace(ipt,@"^.*/b"+id+@"/b/:([^,]+).*", "$1");
- }
- private void SetFalse(string msg)
- {
- if(InfoLbl!=null)
- {
- Message(InfoLbl, msg);
- }
- Valid = false;
- }
- private void SetFocus(Label InfoLbl, string id)
- {
- string scp = @"<script type='text/javascript'>"+
- @"//<![CDATA["+"/r/n"+
- @" var OnloadFunc;"+
- @" try{OnloadFunc = onload}catch(e){}"+
- @" onload = function()"+
- @" {"+
- @" try{OnloadFunc();}catch(e){}"+
- @" var ctl = document.getElementById('"+id+"');"+
- @" if(ctl!=null)"+
- @" {"+
- @" ctl.focus();"+
- @" }"+
- @" }"+
- @"//]]>"+
- @"</"+
- @"script>";
- if(InfoLbl!=null)
- {
- InfoLbl.Text += scp;
- }
- } // end private void SetFocus
- private void Message(Label InfoLabel, string msg)
- {
- InfoLabel.Text +="<div style=/"display:table;width:100%;background-color:yellow!important;";
- InfoLabel.Text +="color:black!important;text-align:center!important;margin:10px 0px;/">";
- InfoLabel.Text += msg+"</div>";
- }
- private void Message(string msg, Label InfoLabel)
- {
- Message(InfoLabel, msg);
- }
- private void Message(Label InfoLabel, string msg, string charSet)
- {
- InfoLabel.Text+="<meta http-equiv=/"Content-Type/" content=/"text/html; charset="+
- charSet+"/" />";
- Message(InfoLabel, msg);
- }
- //-----------------------------------end private method
- //-----------------------------------begin public property
- //-----------------------------------end public property
- //-----------------------------------begin private property
- //-----------------------------------end private property
- /*-----------------------------------------------------------------------------------*/
- * shawl.qiu class v RegExp 1.0
- /*-----------------------------------------------------------------------------------*/
- //---------------------------------------------------------------------begin class RegExp
- public class RegExp
- {
- //-----------------------------------begin event
- public RegExp()
- {
- }
- ~RegExp()
- {
- }
- //-----------------------------------end event
- //-----------------------------------begin public constant
- //-----------------------begin about
- public const String auSubject = "shawl.qiu class v RegExp 1.0";
- public const String auVersion = "v1.0";
- public const String au = "shawl.qiu";
- public const String auEmail = "shawl.qiu@gmail.com";
- public const String auBlog = "http://blog.csdn.net/btbtd";
- public const String auCreateDate = "2007-4-19";
- //-----------------------end about
- //-----------------------------------end public constant
- //-----------------------------------begin private constant
- //-----------------------------------end private constant
- //-----------------------------------begin public static method
- public static string Trim(string sForRemove)
- {
- return Regex.Replace(sForRemove, @"^/s+|/s+$", "");
- }
- public static bool IsEmail(string sMail)
- {
- return Regex.IsMatch(sMail, @"@", RegexOptions.IgnoreCase);
- }
- public static bool IsEmpty(string str)
- {
- str = Regex.Replace(str, @"/s+", "");
- if(str=="")return true;
- return false;
- }
- public static bool IsNumber(string str)
- {
- return Regex.IsMatch(str, @"^[0-9.]+$", RegexOptions.IgnoreCase);
- }
- public static string GetBr(string ipt)
- {
- return Regex.Replace(ipt, @"(/r/n)", @"<br/>$1");
- }
- public static string RemoveBr(string ipt)
- {
- return Regex.Replace(ipt, @"(<br/>|<br>|<br />)", @"", RegexOptions.IgnoreCase);
- }
- public static bool Test(string ipt, string ptr, RegexOptions ro)
- {
- return Regex.IsMatch(@ipt, @ptr, ro);
- }
- public static bool Test(string ipt, string ptr)
- {
- return Regex.IsMatch(@ipt, @ptr, RegexOptions.IgnoreCase);
- }
- public static void ClearSpace(ref string ipt)
- {
- ipt = Regex.Replace(ipt, @"/s+", "");
- }
- //-----------------------------------end public static method
- //-----------------------------------begin private static method
- //-----------------------------------end private static method
- //-----------------------------------begin public variable
- //-----------------------------------end public variable
- //-----------------------------------begin private variable
- //-----------------------------------end private variable
- //-----------------------------------begin public method
- //-----------------------------------end public method
- //-----------------------------------begin private method
- //-----------------------------------end private method
- //-----------------------------------begin public property
- //-----------------------------------end public property
- //-----------------------------------begin private property
- //-----------------------------------end private property
- }
- //---------------------------------------------------------------------end class RegExp
- }
- //---------------------------------------------------------------------end class FormValidator