开发者社区> 技术小胖子> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

C#中验证控件的使用方法总结一

简介:
+关注继续查看


    

   用户在控件中输入数据,往往需要对其进行有效性难,这样可以及时检查用户提交的数据是否合法,同时可以减少服务器的负担。
A  ASP.NET对验证控件的支持
ASP.NET为开发人员提供了一整套完整的服务器控件来验证用户输入的信息是否有效。其中最主要的验证控件如下:
1
RequiredFieldValidator:验证一个必填字段,如果这个字段没填,那么,将不能提交信息。
2CompareValidator:比较验证。比较两个字段值是否相等,如密码和确认密码两个字段是否相等;比较一个字段与一个具体的值。
3RangeValidator:范围验证。验证一个字段是否在某个范围中,如成绩字段要是0~100范围中。
4RegularExpressionValidator:正则表达式验证。它根据正则表达式来验证用户输入字段的格式是否合法,如电子邮件、身份证、电话号码等。
5CustomValidator:在运行定制的客户端JavaScriptVBScript函数时,可以使用这个控件。
使用此类控件的步骤如下:
(1)       打开VS2008,新建一ASP.NET WEB应用程序,并命名为ValidatorTest
(2)       切换页面到“设计”视图,添加一4*3的列表
(3)       将表中拖放三个Label3TextBox1Button控件,为3LabelButton控件添加Text属性。依次为“用户名:”、“密码:”、“年龄”、“提交”
(4)       分别为3TextBox控件设置ID属性,其中,用户名为UserNameText;密码为PwdText;年龄为AgeText
(5)       在“工具箱”中的验证栏找到RequiredFiledValidator控件,拖放至用户名文本框右边表格中
(6)       选定该控件,将ErrorMessage设置为“*用户名为必填项”,同时ControlToValidate属性设置为UserNameText
RequiredFiledValidator控件还有一个比较重要的属性display,它有三个可取值Dynamic(动态显示错误信息,其在页面上的位置只有在验证错误发生时才给出)None(验证错误信息,但不显示)Static(不管验证错误何时发生,页面都会留出供显示错误信息的)。B RegularExpressionValidator正则表达式验证控件
RegularExpressionValidator正则表达式验证控件的功能非常强大,可以自己容易构造验证方式,我们先来看看标准代码:
 
ASP:RegularExpressionValidator id="Validator_ID" RunAt="Server"
ControlToValidate="要验证控件名"
ValidationExpression="正则表达式"
errorMessage="错误信息"
display="Static"
现在来看看它的构造:在ValidationExpression中,不同的字符表示不同的含义:"."表示任意字符;"*"表示和其他表达式一起,表示容易组合;"[A-Z]"表示任意大写字母;"\d"表示容易一个数字;注意,在以上表达式中,引号不包括在内;
常见正则表达式主要有:
这是我曾经按照网上的资料自己做的一个验证控件的例子,看到switch,每个case中的绿字,即为可验证的类型,暗棕色的即为其正则表达式。
InBlock.gif/*---------------------------------------------------------------- 
InBlock.gif 
InBlock.gif        // Copyright (C) ** Project 
InBlock.gif 
InBlock.gif        // 版权所有。 
InBlock.gif 
InBlock.gif        // 文件名:RegularExpressionInput 
InBlock.gif 
InBlock.gif        // 文件功能描述:对种常见输入进行验证 
InBlock.gif 
InBlock.gif 
InBlock.gif        // 创建标识: 
InBlock.gif 
InBlock.gif        // 
InBlock.gif 
InBlock.gif        // 修改标识: 
InBlock.gif 
InBlock.gif        // 修改描述: 
InBlock.gif 
InBlock.gif----------------------------------------------------------------*/
 
InBlock.gif 
InBlock.gif 
InBlock.gif 
InBlock.gif..... 
InBlock.gif 
InBlock.gif 
InBlock.gif 
InBlock.gifnamespace XEP.UI 
InBlock.gif 
InBlock.gif
InBlock.gif 
InBlock.gif        public partial class RegularExpressionInput : System.Web.UI.UserControl 
InBlock.gif 
InBlock.gif        { 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif 
InBlock.gif                /// 验证类型枚举 
InBlock.gif 
InBlock.gif                /// </summary> 
InBlock.gif 
InBlock.gif                public enum validation 
InBlock.gif 
InBlock.gif                { 
InBlock.gif 
InBlock.gif                        整数, 
InBlock.gif 
InBlock.gif                        正整数, 
InBlock.gif 
InBlock.gif                        负整数, 
InBlock.gif 
InBlock.gif                        数字, 
InBlock.gif 
InBlock.gif                        电话或手机, 
InBlock.gif 
InBlock.gif                        正数, 
InBlock.gif 
InBlock.gif                        负数, 
InBlock.gif 
InBlock.gif                        浮点数, 
InBlock.gif 
InBlock.gif                        正浮点数, 
InBlock.gif 
InBlock.gif                        负浮点数, 
InBlock.gif 
InBlock.gif                        浮点数, 
InBlock.gif 
InBlock.gif                        非负浮点数, 
InBlock.gif 
InBlock.gif                        非正浮点数, 
InBlock.gif 
InBlock.gif                        邮件, 
InBlock.gif 
InBlock.gif                        颜色, 
InBlock.gif 
InBlock.gif                        url, 
InBlock.gif 
InBlock.gif                        中文, 
InBlock.gif 
InBlock.gif                        ACSII字符, 
InBlock.gif 
InBlock.gif                        邮编, 
InBlock.gif 
InBlock.gif                        手机, 
InBlock.gif 
InBlock.gif                        IP地址, 
InBlock.gif 
InBlock.gif                        非空, 
InBlock.gif 
InBlock.gif                        图片, 
InBlock.gif 
InBlock.gif                        压缩文件, 
InBlock.gif 
InBlock.gif                        日期, 
InBlock.gif 
InBlock.gif                        QQ号码, 
InBlock.gif 
InBlock.gif                        国内电话, 
InBlock.gif 
InBlock.gif                        用户名, 
InBlock.gif 
InBlock.gif                        字母, 
InBlock.gif 
InBlock.gif                        大写字母, 
InBlock.gif 
InBlock.gif                        小写字母, 
InBlock.gif 
InBlock.gif                        身份证 
InBlock.gif 
InBlock.gif                } 
InBlock.gif 
InBlock.gif    
InBlock.gif 
InBlock.gif                private validation val; 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif 
InBlock.gif                /// 验证类型 
InBlock.gif 
InBlock.gif                /// </summary> 
InBlock.gif 
InBlock.gif                public validation Validation 
InBlock.gif 
InBlock.gif                { 
InBlock.gif 
InBlock.gif                        get { return val; } 
InBlock.gif 
InBlock.gif                        set { val = value; } 
InBlock.gif 
InBlock.gif                } 
InBlock.gif 
InBlock.gif    
InBlock.gif 
InBlock.gif                private string errorMsg; 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif 
InBlock.gif                /// 出错提示 
InBlock.gif 
InBlock.gif                /// </summary> 
InBlock.gif 
InBlock.gif                public string ErrorMsg 
InBlock.gif 
InBlock.gif                { 
InBlock.gif 
InBlock.gif                        get { return errorMsg; } 
InBlock.gif 
InBlock.gif                        set { errorMsg = value; } 
InBlock.gif 
InBlock.gif                } 
InBlock.gif 
InBlock.gif    
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif 
InBlock.gif                /// 输入文本 
InBlock.gif 
InBlock.gif                /// </summary> 
InBlock.gif 
InBlock.gif                public string Text 
InBlock.gif 
InBlock.gif                { 
InBlock.gif 
InBlock.gif                        get { return this.TextBox1.Text; } 
InBlock.gif 
InBlock.gif                        set { this.TextBox1.Text = value; } 
InBlock.gif 
InBlock.gif                } 
InBlock.gif 
InBlock.gif    
InBlock.gif 
InBlock.gif                private System.Drawing.Color msgColor; 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif 
InBlock.gif                /// 错误提示颜色 
InBlock.gif 
InBlock.gif                /// </summary> 
InBlock.gif 
InBlock.gif                public System.Drawing.Color MsgColor 
InBlock.gif 
InBlock.gif                { 
InBlock.gif 
InBlock.gif                        get { return msgColor; } 
InBlock.gif 
InBlock.gif                        set { msgColor = value; } 
InBlock.gif 
InBlock.gif                } 
InBlock.gif 
InBlock.gif    
InBlock.gif 
InBlock.gif                protected string color; 
InBlock.gif 
InBlock.gif                protected string pattern; 
InBlock.gif 
InBlock.gif                protected void Page_Load(object sender, EventArgs e) 
InBlock.gif 
InBlock.gif                { 
InBlock.gif 
InBlock.gif                        switch (Convert.ToInt32(val)) 
InBlock.gif 
InBlock.gif                        { 
InBlock.gif 
InBlock.gif                                case 0://整数 
InBlock.gif 
InBlock.gif                                        pattern = "/^-?[1-9]\\d*$/"
InBlock.gif 
InBlock.gif                                        break
InBlock.gif 
InBlock.gif                                case 1://正整数 
InBlock.gif 
InBlock.gif                                        pattern = "/^[1-9]\\d*$/"
InBlock.gif 
InBlock.gif                                        break
InBlock.gif 
InBlock.gif                                case 2://负整数 
InBlock.gif 
InBlock.gif                                        pattern = "/^-[1-9]\\d*$/"
InBlock.gif 
InBlock.gif                                        break
InBlock.gif 
InBlock.gif                                case 3://数字 
InBlock.gif 
InBlock.gif                                        pattern = "/^([+-]?)\\d*\\.?\\d+$/"
InBlock.gif 
InBlock.gif                                        break
InBlock.gif 
InBlock.gif                                case 4://电话或手机 
InBlock.gif 
InBlock.gif                                        pattern = "/d*\\-?\\d+$/"
InBlock.gif 
InBlock.gif                                        break
InBlock.gif 
InBlock.gif                                case 5://正数(正整数+ 0) 
InBlock.gif 
InBlock.gif                                        pattern = "/^[1-9]\\d*|0$/"
InBlock.gif 
InBlock.gif                                        break
InBlock.gif 
InBlock.gif                                case 6://负数(负整数+ 0) 
InBlock.gif 
InBlock.gif                                        pattern = "/^-[1-9]\\d*|0$/"
InBlock.gif 
InBlock.gif                                        break
InBlock.gif 
InBlock.gif                                case 7://浮点数 
InBlock.gif 
InBlock.gif                                        pattern = "/^([+-]?)\\d*\\.\\d+$/"
InBlock.gif 
InBlock.gif                                        break
InBlock.gif 
InBlock.gif                                case 8://正浮点数 
InBlock.gif 
InBlock.gif                                        pattern = "/^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*$/"
InBlock.gif 
InBlock.gif                                        break
InBlock.gif 
InBlock.gif                                case 9://负浮点数 
InBlock.gif 
InBlock.gif                                        pattern = "/^-([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*)$/"
InBlock.gif 
InBlock.gif                                        break
InBlock.gif 
InBlock.gif                                case 10://浮点数 
InBlock.gif 
InBlock.gif                                        pattern = "/^-?([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*|0?.0+|0)$/"
InBlock.gif 
InBlock.gif                                        break
InBlock.gif 
InBlock.gif                                case 11://非负浮点数(正浮点数+ 0) 
InBlock.gif 
InBlock.gif                                        pattern = "/^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*|0?.0+|0$/"
InBlock.gif 
InBlock.gif                                        break
InBlock.gif 
InBlock.gif                                case 12://非正浮点数(负浮点数+ 0) 
InBlock.gif 
InBlock.gif                                        pattern = "/^(-([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*))|0?.0+|0$/"
InBlock.gif 
InBlock.gif                                        break
InBlock.gif 
InBlock.gif                                case 13://邮件 
InBlock.gif 
InBlock.gif                                        pattern = "/^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$/"
InBlock.gif 
InBlock.gif                                        break
InBlock.gif 
InBlock.gif                                case 14://颜色 
InBlock.gif 
InBlock.gif                                        pattern = "/^[a-fA-F0-9]{6}$/"
InBlock.gif 
InBlock.gif                                        break
InBlock.gif 
InBlock.gif                                case 15://url(http格式的) 
InBlock.gif 
InBlock.gif                                        pattern = "/^http[s]?:\\/\\/([\\w-]+\\.)+[\\w-]+([\\w-.\\/?%&=]*)?$/"
InBlock.gif 
InBlock.gif                                        break
InBlock.gif 
InBlock.gif                                case 16://仅中文 
InBlock.gif 
InBlock.gif                                        pattern = "/^[\\u4E00-\\u9FA5\\uF900-\\uFA2D]+$/"
InBlock.gif 
InBlock.gif                                        break
InBlock.gif 
InBlock.gif                                case 17://仅ACSII字符 
InBlock.gif 
InBlock.gif                                        pattern = "/^[\\x00-\\xFF]+$/"
InBlock.gif 
InBlock.gif                                        break
InBlock.gif 
InBlock.gif                                case 18://邮编 
InBlock.gif 
InBlock.gif                                        pattern = "/^\\d{6}$/"
InBlock.gif 
InBlock.gif                                        break
InBlock.gif 
InBlock.gif                                case 19://手机(13号段和号段) 
InBlock.gif 
InBlock.gif                                        pattern = "/^(13|15)[0-9]{9}$/"
InBlock.gif 
InBlock.gif                                        break
InBlock.gif 
InBlock.gif                                case 20://ip地址 
InBlock.gif 
InBlock.gif                                        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])$/"
InBlock.gif 
InBlock.gif                                        break
InBlock.gif 
InBlock.gif                                case 21://非空 
InBlock.gif 
InBlock.gif                                        pattern = "/^\\S+$/"
InBlock.gif 
InBlock.gif                                        break
InBlock.gif 
InBlock.gif                                case 22://图片 
InBlock.gif 
InBlock.gif                                        pattern = "/(.*)\\.(jpg|bmp|gif|ico|pcx|jpeg|tif|png|raw|tga)$/"
InBlock.gif 
InBlock.gif                                        break
InBlock.gif 
InBlock.gif                                case 23://压缩文件 
InBlock.gif 
InBlock.gif                                        pattern = "/(.*)\\.(rar|zip|7zip|tgz)$/"
InBlock.gif 
InBlock.gif                                        break
InBlock.gif 
InBlock.gif                                case 24://日期 
InBlock.gif 
InBlock.gif                                        pattern = "/^\\d{4}(\\-|\\/|\\.)\\d{1,2}\\1\\d{1,2}$/"
InBlock.gif 
InBlock.gif                                        break
InBlock.gif 
InBlock.gif                                case 25://QQ号码 
InBlock.gif 
InBlock.gif                                        pattern = "/^[1-9]*[1-9][0-9]*$/"
InBlock.gif 
InBlock.gif                                        break
InBlock.gif 
InBlock.gif                                case 26://国内电话 
InBlock.gif 
InBlock.gif                                        pattern = "/(\\d{3}-|\\d{4}-)?(\\d{8}|\\d{7})/"
InBlock.gif 
InBlock.gif                                        break
InBlock.gif 
InBlock.gif                                case 27://用来用户注册。匹配由数字、个英文字母或者下划线组成的字符串 
InBlock.gif 
InBlock.gif                                        pattern = "/^(?!\\d)[a-zA-Z0-9\\u4e00-\\u9fa5]+$/"
InBlock.gif 
InBlock.gif                                        break
InBlock.gif 
InBlock.gif                                case 28://字母 
InBlock.gif 
InBlock.gif                                        pattern = "/^[A-Za-z]+$/"
InBlock.gif 
InBlock.gif                                        break
InBlock.gif 
InBlock.gif                                case 29://大写字母 
InBlock.gif 
InBlock.gif                                        pattern = "/^[A-Z]+$/"
InBlock.gif 
InBlock.gif                                        break
InBlock.gif 
InBlock.gif                                case 30://小写字母 
InBlock.gif 
InBlock.gif                                        pattern = "/^[a-z]+$/"
InBlock.gif 
InBlock.gif                                        break
InBlock.gif 
InBlock.gif                                case 31://身份证 
InBlock.gif 
InBlock.gif                                        pattern = "/^[1-9]([0-9]{14}|[0-9]{17})$/"
InBlock.gif 
InBlock.gif                                        break
InBlock.gif 
InBlock.gif                                default
InBlock.gif 
InBlock.gif                                        pattern = string.Empty; 
InBlock.gif 
InBlock.gif                                        break
InBlock.gif 
InBlock.gif                        } 
InBlock.gif 
InBlock.gif
InBlock.gif 
InBlock.gif         
InBlock.gif 
InBlock.gif        } 
InBlock.gif 
InBlock.gif
InBlock.gif 
上面的程序只是为了表示验证某种类型,其正则表达式的形式,本身没有什么可运行性。在“正则表达式编辑器中”分别选择标准表达式,输入下面的验证表达式即可。
 
未完,因多于8万字符,分两节 
 




     本文转自 gaochaojs 51CTO博客,原文链接:http://blog.51cto.com/jncumter/184783,如需转载请自行联系原作者

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
【牛刀小试】——Repeater控件常见错误及几种相似控件的区别
【牛刀小试】——Repeater控件常见错误及几种相似控件的区别
0 0
WPF Adorner+附加属性 实现控件友好提示
原文:WPF Adorner+附加属性 实现控件友好提示 标题太空泛,直接上图   无论是在验证啊,还是提示方面等一些右上角的角标之类的效果,我们会怎么做? 这里介绍一种稍微简单一些的方法,利用附加属性和Adorner来完成。
496 0
WinForm自定义验证控件
本文转载:http://blog.csdn.net/ziyouli/article/details/7583824 此篇博文不错:http://blog.csdn.net/sony0732/article/details/2502858   该控件使用的是visual studio2010开发,对TextBox进行了改写,附带了验证功能,不需要开发人员再次对TextBox的内容进行验证,也不需要在相关的按钮里写判断语句,节省了对内容验证的时间,下面为大家介绍下控件的功能和用法。
706 0
WdatePicker日历控件使用方法
1. 跨无限级框架显示   无论你把日期控件放在哪里,你都不需要担心会被外层的iframe所遮挡进而影响客户体验,因为My97日期控件是可以跨无限级框架显示的   示例2-7 跨无限级框架演示 可无限跨越框架iframe,无论怎么嵌套框架都不必担心了,即使有滚动条也不怕   2.
685 0
easyui事件和方法的使用方法
图片是easyui combox 文档的部分截图 Js代码  alert("相似度为:"+$("#select_percent").combobox("getValue"));     如其中的onChange事件 Js代码   $('#select_percent').
678 0
[自定义服务器控件] 第一步:文本框。
最近在整理我写的几个服务器控件,发出来与大家共享吧。 我写的自定义服务器控件呢分为两个类, 一是“简单继承”控件,就是继承框架里的控件然后加点属性了、事件了什么的。 二是“功能”控件,简单的说那是符合控件,但是呢是以实现一种功能为目的地。
475 0
文章
问答
文章排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载