zoukankan      html  css  js  c++  java
  • asp.net验证控件用法

    一、使用图片来显示错误信息
    <body>
        
    <form id="form1" runat="server">
            
    <asp:TextBox ID="txtInt" runat="server"></asp:TextBox>
            
            
    <asp:CustomValidator ID="CustomValidator1" runat="server" 
              ClientValidationFunction
    ="validate_Integer" Display="Dynamic" 
              ErrorMessage
    ="<img src='Images/check_error.gif' style='13px;height:13px;'>" >
            
    </asp:CustomValidator><br />
            
            
    <asp:Button ID="Button1" runat="server" Text="Button" />
        
    </form>
    </body>

    其中调用javascript函数validate_Integer,所以在<head></head>之间插入脚本代码
    <script type="text/javascript" language="javascript">
     function isInteger(s)
     {
          var i; 
          
    for (i = 0; i < s.length; i++)
          { 
    //检测字符是否为数字
              var c = s.charAt(i);
             
    if (((c < "0"|| (c > "9"))) return false;
          } 
         
    return true;
    }

    function validate_Integer(source, args)
    {
         var txtintval
    =document.getElementById("<%= txtInt.ClientID %>");
         
    if(!isInteger (txtintval.value))
         { 
           args.IsValid
    =false;
         }
         
    else
         { 
          args.IsValid
    =true;
         } 
    }
    </script>


    二、使用RequiredFieldValidator进行非空验证
    1、传统的验证方式
    通过设置其ErrorMessage属性指定验证错误提示信息
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>&nbsp;
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
     ControlToValidate
    ="TextBox1" EnableClientScript="False"  ErrorMessage="*输入字段不能为空!" >
    </asp:RequiredFieldValidator>

    2.弹出JavaScript的提示窗口
    只需要将验证控件的Text属性赋上一句JavaScript代码
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>&nbsp;
    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
        ControlToValidate
    ="TextBox2" EnableClientScript="False" ErrorMessage="RequiredFieldValidator"
        Text
    ="<script type='text/javascript'>alert('*输入字段不能为空!')</script>"  >
    </asp:RequiredFieldValidator>

    3.带声音提示的验证,此时也可以用图片做为错误提示信息
    <body>
        
    <form id="form1" runat="server">
            
    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>&nbsp;
            
            
    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
                ControlToValidate
    ="TextBox3"
                EnableClientScript
    ="False"
                ErrorMessage
    ="RequiredFieldValidator"
                Text
    ="<bgsound src='demo1.wav'>">
            
    </asp:RequiredFieldValidator>

            
    <asp:Button ID="Button1" runat="server" Text="Button" />
        
    </form>
    </body>


     来自:http://www.cnblogs.com/beniao/archive/2008/07/06/1236330.html
  • 相关阅读:
    Web.config配置详解
    vs2010下创建webservice
    WinForm如何调用Web Service
    Android动画的两种使用方式。
    ES6介绍
    django批量form表单处理
    django生命周期示意图
    django中的构造字典(二级菜单,评论树,购物车)
    django中介模型,CBV模型,及logging日志配制
    django中csrftoken跨站请求伪造的几种方式
  • 原文地址:https://www.cnblogs.com/tuzhiye/p/1381707.html
Copyright © 2011-2022 走看看