|
今天用正则表达式时突然来了兴致,想自己做一个验证用户输入的用户控件,经过几个小时的努力,终于完成了,内置了包括整数,正整数,负整数,数字,电话或手机,正数,负数,浮点数, 正浮点数,负浮点数,浮点数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 { /// <summary> /// 验证类型枚举 /// </summary> public enum validation { 整数, 正整数, 负整数, 数字, 电话或手机, 正数, 负数, 浮点数, 正浮点数, 负浮点数, 浮点数2, 非负浮点数, 非正浮点数, 邮件, 颜色, url, 中文, ACSII字符, 邮编, 手机, IP地址, 非空, 图片, 压缩文件, 日期, QQ号码, 国内电话, 用户名, 字母, 大写字母, 小写字母, 身份证 }
private validation val; /// <summary> /// 验证类型 /// </summary> public validation Validation { get { return val; } set { val = value; } }
private string errorMsg; /// <summary> /// 出错提示 /// </summary> public string ErrorMsg { get { return errorMsg; } set { errorMsg = value; } }
/// <summary> /// 输入文本 /// </summary> public string Text { get { return this.TextBox1.Text; } set { this.TextBox1.Text = value; } }
private System.Drawing.Color msgColor; /// <summary> /// 错误提示颜色 /// </summary> public System.Drawing.Color MsgColor { get { return msgColor; } set { msgColor = value; } }
共3页: 上一页 1 [2] [3] 下一页
|