今天用正则表达式时突然来了兴致,想自己做一个验证用户输入的用户控件,经过几个小时的努力,终于完成了,内置了包括整数,正整数,负整数,数字,电话或手机,正数,负数,浮点数, 正浮点数,负浮点数,浮点数2,非负浮点数, 非正浮点数, 邮件,颜色,url,中文, ACSII字符,邮编,手机,IP地址,非空,图片,压缩文件, 日期, QQ号码,国内电话, 用户名,字母,大写字母,小写字母,身份证的32种常见的用户输入验证。先把代码贴出来再详细说说:
RegularExpressionInput.ascx代码:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="RegularExpressionInput.ascx.cs" Inherits="WebApplication1.RegularExpressionInput" %>
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://正数(正整数 + 0)
pattern = "/^[1-9]\\d*|0$/";
break;
case 6://负数(负整数 + 0)
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://非负浮点数(正浮点数 + 0)
pattern = "/^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*|0?.0+|0$/";
break;
case 12://非正浮点数(负浮点数 + 0)
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://url(http格式的)
pattern = "/^http[s]?:\\/\\/([\\w-]+\\.)+[\\w-]+([\\w-.\\/?%&=]*)?$/";
break;
case 16://仅中文
pattern = "/^[\\u4E00-\\u9FA5\\uF900-\\uFA2D]+$/";
break;
case 17://仅ACSII字符
pattern = "/^[\\x00-\\xFF]+$/";
break;
case 18://邮编
pattern = "/^\\d{6}$/";
break;
case 19://手机(13号段和15号段)
pattern = "/^(13|15)[0-9]{9}$/";
break;
case 20://ip地址
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://QQ号码
pattern = "/^[1-9]*[1-9][0-9]*$/";
break;
case 26://国内电话
pattern = "/(\\d{3}-|\\d{4}-)?(\\d{8}|\\d{7})/";
break;
case 27://用来用户注册。匹配由数字、26个英文字母或者下划线组成的字符串
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);
}
///
/// 为所有具有CausesValidation属性的控件添加检查脚本
///
///
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" %>
无标题页
后台代码:
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,这样一来使得源代码大大增加,这是这个控件的一个缺陷,还有待修正。此控件可以在属性窗口处设置出错提示,提示颜色及验证类型,使用起来还是挺方便的,希望大家多提宝贵意见
C#的一个输入验证控件