当前位置:   article > 正文

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

usercontrol 验证结果如何在mainview中使用时提示


    

   用户在控件中输入数据,往往需要对其进行有效性难,这样可以及时检查用户提交的数据是否合法,同时可以减少服务器的负担。
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.gif namespace 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,如需转载请自行联系原作者

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/701471
推荐阅读
相关标签
  

闽ICP备14008679号