今天用正则表达式时突然来了兴致,想自己做一个验证用户输入的用户控件,经过几个小时的努力,终于完成了,内置了包括整数,正整数,负整数,数字,电话或手机,正数,负数,浮点数, 正浮点数,负浮点数,浮点数2,非负浮点数, 非正浮点数, 邮件,颜色,url,中文, ACSII字符,邮编,手机,IP地址,非空,图片,压缩文件, 日期, QQ号码,国内电话, 用户名,字母,大写字母,小写字母,身份证的32种常见的用户输入验证。先把代码贴出来再详细说说:
RegularExpressionInput.ascx代码:
- <%@ControlLanguage="C#"AutoEventWireup="true"CodeBehind="RegularExpressionInput.ascx.cs"Inherits="WebApplication1.RegularExpressionInput"%>
- <asp:TextBoxID="TextBox1"runat="server"></asp:TextBox><spanid="<%=this.ClientID%>_error"style="color:<%=color%>"></span>
RegularExpressionInput.cs代码:
- usingSystem;
- usingSystem.Collections;
- usingSystem.Configuration;
- usingSystem.Data;
- usingSystem.Linq;
- usingSystem.Web;
- usingSystem.Web.Security;
- usingSystem.Web.UI;
- usingSystem.Web.UI.HtmlControls;
- usingSystem.Web.UI.WebControls;
- usingSystem.Web.UI.WebControls.WebParts;
- usingSystem.Reflection;
- namespaceWebApplication1
- {
- publicpartialclassRegularExpressionInput:System.Web.UI.UserControl
- {
-
-
-
- publicenumvalidation
- {
- 整数,
- 正整数,
- 负整数,
- 数字,
- 电话或手机,
- 正数,
- 负数,
- 浮点数,
- 正浮点数,
- 负浮点数,
- 浮点数2,
- 非负浮点数,
- 非正浮点数,
- 邮件,
- 颜色,
- url,
- 中文,
- ACSII字符,
- 邮编,
- 手机,
- IP地址,
- 非空,
- 图片,
- 压缩文件,
- 日期,
- QQ号码,
- 国内电话,
- 用户名,
- 字母,
- 大写字母,
- 小写字母,
- 身份证
- }
- privatevalidationval;
-
-
-
- publicvalidationValidation
- {
- get{returnval;}
- set{val=value;}
- }
- privatestringerrorMsg;
-
-
-
- publicstringErrorMsg
- {
- get{returnerrorMsg;}
- set{errorMsg=value;}
- }
-
-
-
- publicstringText
- {
- get{returnthis.TextBox1.Text;}
- set{this.TextBox1.Text=value;}
- }
- privateSystem.Drawing.ColormsgColor;
-
-
-
- publicSystem.Drawing.ColorMsgColor
- {
- get{returnmsgColor;}
- set{msgColor=value;}
- }
- protectedstringcolor;
- protectedstringpattern;
- protectedvoidPage_Load(objectsender,EventArgse)
- {
- switch(Convert.ToInt32(val))
- {
- case0:
- pattern="/^-?[1-9]\\d*$/";
- break;
- case1:
- pattern="/^[1-9]\\d*$/";
- break;
- case2:
- pattern="/^-[1-9]\\d*$/";
- break;
- case3:
- pattern="/^([+-]?)\\d*\\.?\\d+$/";
- break;
- case4:
- pattern="/d*\\-?\\d+$/";
- break;
- case5:
- pattern="/^[1-9]\\d*|0$/";
- break;
- case6:
- pattern="/^-[1-9]\\d*|0$/";
- break;
- case7:
- pattern="/^([+-]?)\\d*\\.\\d+$/";
- break;
- case8:
- pattern="/^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*$/";
- break;
- case9:
- pattern="/^-([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*)$/";
- break;
- case10:
- pattern="/^-?([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*|0?.0+|0)$/";
- break;
- case11:
- pattern="/^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*|0?.0+|0$/";
- break;
- case12:
- pattern="/^(-([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*))|0?.0+|0$/";
- break;
- case13:
- pattern="/^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$/";
- break;
- case14:
- pattern="/^[a-fA-F0-9]{6}$/";
- break;
- case15:
- pattern="/^http[s]?:\\/\\/([\\w-]+\\.)+[\\w-]+([\\w-.\\/?%&=]*)?$/";
- break;
- case16:
- pattern="/^[\\u4E00-\\u9FA5\\uF900-\\uFA2D]+$/";
- break;
- case17:
- pattern="/^[\\x00-\\xFF]+$/";
- break;
- case18:
- pattern="/^\\d{6}$/";
- break;
- case19:
- pattern="/^(13|15)[0-9]{9}$/";
- break;
- case20:
- pattern="/^(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5]).(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5]).(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5]).(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])$/";
- break;
- case21:
- pattern="/^\\S+$/";
- break;
- case22:
- pattern="/(.*)\\.(jpg|bmp|gif|ico|pcx|jpeg|tif|png|raw|tga)$/";
- break;
- case23:
- pattern="/(.*)\\.(rar|zip|7zip|tgz)$/";
- break;
- case24:
- pattern="/^\\d{4}(\\-|\\/|\\.)\\d{1,2}\\1\\d{1,2}$/";
- break;
- case25:
- pattern="/^[1-9]*[1-9][0-9]*$/";
- break;
- case26:
- pattern="/(\\d{3}-|\\d{4}-)?(\\d{8}|\\d{7})/";
- break;
- case27:
- pattern="/^(?!\\d)[a-zA-Z0-9\\u4e00-\\u9fa5]+$/";
- break;
- case28:
- pattern="/^[A-Za-z]+$/";
- break;
- case29:
- pattern="/^[A-Z]+$/";
- break;
- case30:
- pattern="/^[a-z]+$/";
- break;
- case31:
- pattern="/^[1-9]([0-9]{14}|[0-9]{17})$/";
- break;
- default:
- pattern=string.Empty;
- break;
- }
- color = msgColor.IsEmpty ? System.Drawing.Color.Red.Name : "#" + msgColor.Name.Substring(2);
- TextBox1.Attributes["onkeyup"]+="if(this.value.match("+pattern+")){varerr=document.getElementById('"+
- this.ClientID+"_error');err.innerHTML='';returntrue;}else{varerr=document.getElementById('"+this.ClientID+"_error');err.innerHTML=\""+
- (string.IsNullOrEmpty(errorMsg)?"*":errorMsg)+"\";returnfalse;}";
- AddAttributes(Page);
- }
-
-
-
-
- privatevoidAddAttributes(Controlctrl)
- {
- if(ctrl.HasControls())
- {
- foreach(Controlcinctrl.Controls)
- {
- Typet=c.GetType();
- PropertyInfocauseVal=t.GetProperty("CausesValidation",typeof(bool));
- if(causeVal!=null)
- {
- PropertyInfoattr=t.GetProperty("Attributes",typeof(AttributeCollection));
- AttributeCollectionattributes=(AttributeCollection)attr.GetValue(c,null);
- attributes["onclick"]+="vartxt=document.getElementById('"+this.TextBox1.ClientID+"');if(txt.value.match("+pattern+")){varerr=document.getElementById('"+
- this.ClientID+"_error');err.innerHTML='';returntrue;}else{varerr=document.getElementById('"+this.ClientID+"_error');err.innerHTML=\""+
- (string.IsNullOrEmpty(errorMsg)?"*":errorMsg)+"\";returnfalse;}";
- }
- AddAttributes(c);
- }
- }
- }
- }
- }
测试网页前台代码:
- <%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="Default2.aspx.cs"Inherits="WebApplication1.gy.Default2"%>
- <%@RegisterSrc="~/RegularExpressionInput.ascx"TagName="RegularExpressionInput"
- TagPrefix="rei"%>
- <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <htmlxmlns="http://www.w3.org/1999/xhtml">
- <headrunat="server">
- <title>无标题页</title>
- </head>
- <body>
- <formid="form1"runat="server">
- <div>
- <asp:ButtonID="Button1"runat="server"Text="Button"OnClick="Button1_Click"/>
- <asp:LinkButtonID="LinkButton1"runat="server"OnClick="LinkButton1_Click">LinkButton</asp:LinkButton>
- <rei:RegularExpressionInputID="RegularExpressionInput1"runat="server"ErrorMsg="请输入数字"
- MsgColor="#FF99FF"Validation="数字"/>
- <rei:RegularExpressionInputID="RegularExpressionInput2"runat="server"ErrorMsg="请输入中文"
- MsgColor="#333399"Validation="中文"/>
- </div>
- </form>
- </body>
- </html>
后台代码:
- usingSystem;
- usingSystem.Collections;
- usingSystem.Configuration;
- usingSystem.Data;
- usingSystem.Linq;
- usingSystem.Web;
- usingSystem.Web.Security;
- usingSystem.Web.UI;
- usingSystem.Web.UI.HtmlControls;
- usingSystem.Web.UI.WebControls;
- usingSystem.Web.UI.WebControls.WebParts;
- usingSystem.Xml.Linq;
- namespaceWebApplication1
- {
- publicpartialclassDefault2:System.Web.UI.Page
- {
- protectedvoidPage_Load(objectsender,EventArgse)
- {
- }
- protectedvoidButton1_Click(objectsender,EventArgse)
- {
- Response.Redirect("~/Default.aspx");
- }
- protectedvoidLinkButton1_Click(objectsender,EventArgse)
- {
- Response.Write("Hello");
- }
- }
- }
做这个控件,验证部分不难,无非就是正则表达式而已,难的是这个控件要另页面上所有回发控件在回发之前先进行验证判断,如果验证不通过则不能回发。这里我采取的是遍历页面所有控件,如果控件具有CausesValidation属性,则在回发之前先进行输入验证,通过返回值决定是否回发。另外还有一个难点就是如果存在多个验证控件的时候如何控制页面上的回发控件都能都每一个验证控件进行验证,我现在的做法是将页面所有回发控件的onclick都加上了验证js,这样一来使得源代码大大增加,这是这个控件的一个缺陷,还有待修正。此控件可以在属性窗口处设置出错提示,提示颜色及验证类型,使用起来还是挺方便的,希望大家多提宝贵意见