今天用正则表达式时突然来了兴致,想自己做一个验证用户输入的用户控件,经过几个小时的努力,终于完成了,内置了包括整数,正整数,负整数,数字,电话或手机,正数,负数,浮点数, 正浮点数,负浮点数,浮点数2,非负浮点数, 非正浮点数, 邮件,颜色,url,中文, ACSII字符,邮编,手机,IP地址,非空,图片,压缩文件, 日期, QQ号码,国内电话, 用户名,字母,大写字母,小写字母,身份证的32种常见的用户输入验证。先把代码贴出来再详细说说:
RegularExpressionInput.ascx代码:
- <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="RegularExpressionInput.ascx.cs" Inherits="WebApplication1.RegularExpressionInput" %>
- <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><span id="<%= this.ClientID %>_error" style="color:<%= color%>"></span>
RegularExpressionInput.cs代码:
- using System;
- using System.Collections;
- using System.Configuration;
- using System.Data;
- using System.Linq;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.HtmlControls;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Reflection;
- namespace WebApplication1
- {
- public partial class RegularExpressionInput : System.Web.UI.UserControl
- {
-
-
-
- public enum validation
- {
- 整数,
- 正整数,
- 负整数,
- 数字,
- 电话或手机,
- 正数,
- 负数,
- 浮点数,
- 正浮点数,
- 负浮点数,
- 浮点数2,
- 非负浮点数,
- 非正浮点数,
- 邮件,
- 颜色,
- url,
- 中文,
- ACSII字符,
- 邮编,
- 手机,
- IP地址,
- 非空,
- 图片,
- 压缩文件,
- 日期,
- QQ号码,
- 国内电话,
- 用户名,
- 字母,
- 大写字母,
- 小写字母,
- 身份证
- }
- private validation val;
-
-
-
- public validation Validation
- {
- get { return val; }
- set { val = value; }
- }
- private string errorMsg;
-
-
-
- public string ErrorMsg
- {
- get { return errorMsg; }
- set { errorMsg = value; }
- }
-
-
-
- public string Text
- {
- get { return this.TextBox1.Text; }
- set { this.TextBox1.Text = value; }
- }
- private System.Drawing.Color msgColor;
-
-
-
- public System.Drawing.Color MsgColor
- {
- get { return msgColor; }
- set { msgColor = value; }
- }
- protected string color;
- protected string pattern;
- protected void Page_Load(object sender, EventArgs e)
- {
- switch (Convert.ToInt32(val))
- {
- case 0:
- pattern = "/^-?[1-9]//d*$/";
- break;
- case 1:
- pattern = "/^[1-9]//d*$/";
- break;
- case 2:
- pattern = "/^-[1-9]//d*$/";
- break;
- case 3:
- pattern = "/^([+-]?)//d*//.?//d+$/";
- break;
- case 4:
- pattern = "/d*//-?//d+$/";
- break;
- case 5:
- pattern = "/^[1-9]//d*|0$/";
- break;
- case 6:
- pattern = "/^-[1-9]//d*|0$/";
- break;
- case 7:
- pattern = "/^([+-]?)//d*//.//d+$/";
- break;
- case 8:
- pattern = "/^[1-9]//d*.//d*|0.//d*[1-9]//d*$/";
- break;
- case 9:
- pattern = "/^-([1-9]//d*.//d*|0.//d*[1-9]//d*)$/";
- break;
- case 10:
- pattern = "/^-?([1-9]//d*.//d*|0.//d*[1-9]//d*|0?.0+|0)$/";
- break;
- case 11:
- pattern = "/^[1-9]//d*.//d*|0.//d*[1-9]//d*|0?.0+|0$/";
- break;
- case 12:
- pattern = "/^(-([1-9]//d*.//d*|0.//d*[1-9]//d*))|0?.0+|0$/";
- break;
- case 13:
- pattern = "/^//w+((-//w+)|(//.//w+))*//@[A-Za-z0-9]+((//.|-)[A-Za-z0-9]+)*//.[A-Za-z0-9]+$/";
- break;
- case 14:
- pattern = "/^[a-fA-F0-9]{6}$/";
- break;
- case 15:
- pattern = "/^http[s]?://////([//w-]+//.)+[//w-]+([//w-.///?%&=]*)?$/";
- break;
- case 16:
- pattern = "/^[//u4E00-//u9FA5//uF900-//uFA2D]+$/";
- break;
- case 17:
- pattern = "/^[//x00-//xFF]+$/";
- break;
- case 18:
- pattern = "/^//d{6}$/";
- break;
- case 19:
- pattern = "/^(13|15)[0-9]{9}$/";
- break;
- case 20:
- 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;
- case 21:
- pattern = "/^//S+$/";
- break;
- case 22:
- pattern = "/(.*)//.(jpg|bmp|gif|ico|pcx|jpeg|tif|png|raw|tga)$/";
- break;
- case 23:
- pattern = "/(.*)//.(rar|zip|7zip|tgz)$/";
- break;
- case 24:
- pattern = "/^//d{4}(//-|///|//.)//d{1,2}//1//d{1,2}$/";
- break;
- case 25:
- pattern = "/^[1-9]*[1-9][0-9]*$/";
- break;
- case 26:
- pattern = "/(//d{3}-|//d{4}-)?(//d{8}|//d{7})/";
- break;
- case 27:
- pattern = "/^(?!//d)[a-zA-Z0-9//u4e00-//u9fa5]+$/";
- break;
- case 28:
- pattern = "/^[A-Za-z]+$/";
- break;
- case 29:
- pattern = "/^[A-Z]+$/";
- break;
- case 30:
- pattern = "/^[a-z]+$/";
- break;
- case 31:
- 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 + ")){var err=document.getElementById('" +
- this.ClientID + "_error');err.innerHTML='';return true;}else{var err=document.getElementById('" + this.ClientID + "_error');err.innerHTML=/"" +
- (string.IsNullOrEmpty(errorMsg) ? "*" : errorMsg) + "/";return false;}";
- AddAttributes(Page);
- }
-
-
-
-
- private void AddAttributes(Control ctrl)
- {
- if (ctrl.HasControls())
- {
- foreach (Control c in ctrl.Controls)
- {
- Type t = c.GetType();
- PropertyInfo causeVal = t.GetProperty("CausesValidation", typeof(bool));
- if (causeVal != null)
- {
- PropertyInfo attr = t.GetProperty("Attributes", typeof(AttributeCollection));
- AttributeCollection attributes = (AttributeCollection)attr.GetValue(c, null);
- attributes["onclick"] += "var txt = document.getElementById('" + this.TextBox1.ClientID + "');if(txt.value.match(" + pattern + ")){var err=document.getElementById('" +
- this.ClientID + "_error');err.innerHTML='';return true;}else{var err=document.getElementById('" + this.ClientID + "_error');err.innerHTML=/"" +
- (string.IsNullOrEmpty(errorMsg) ? "*" : errorMsg) + "/";return false;}";
- }
- AddAttributes(c);
- }
- }
- }
- }
- }
测试网页前台代码:
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default2.aspx.cs" Inherits="WebApplication1.gy.Default2" %>
- <%@ Register Src="~/RegularExpressionInput.ascx" TagName="RegularExpressionInput"
- TagPrefix="rei" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>无标题页</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
- <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton>
- <rei:RegularExpressionInput ID="RegularExpressionInput1" runat="server" ErrorMsg="请输入数字"
- MsgColor="#FF99FF" Validation="数字" />
- <rei:RegularExpressionInput ID="RegularExpressionInput2" runat="server" ErrorMsg="请输入中文"
- MsgColor="#333399" Validation="中文" />
- </div>
- </form>
- </body>
- </html>
后台代码:
- using System;
- using System.Collections;
- using System.Configuration;
- using System.Data;
- using System.Linq;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.HtmlControls;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Xml.Linq;
- namespace WebApplication1
- {
- public partial class Default2 : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- Response.Redirect("~/Default.aspx");
- }
- protected void LinkButton1_Click(object sender, EventArgs e)
- {
- Response.Write("Hello");
- }
- }
-
- }
做这个控件,验证部分不难,无非就是正则表达式而已,难的是这个控件要另页面上所有回发控件在回发之前先进行验证判断,如果验证不通过则不能回发。这里我采取的是遍历页面所有控件,如果控件具有CausesValidation属性,则在回发之前先进行输入验证,通过返回值决定是否回发。另外还有一个难点就是如果存在多个验证控件的时候如何控制页面上的回发控件都能都每一个验证控件进行验证,我现在的做法是将页面所有回发控件的onclick都加上了验证js,这样一来使得源代码大大增加,这是这个控件的一个缺陷,还有待修正。此控件可以在属性窗口处设置出错提示,提示颜色及验证类型,使用起来还是挺方便的,希望大家多提宝贵意见