ASP.NET验证控件二

2018-06-17 20:57:35来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

RequiredFieldValidator 验证控件

  页面布局:

<div>
        <h1>RequiredFieldValidator 验证控件</h1>
          用户名:
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="TextBox1" runat="server" Display="Dynamic"
ErrorMessage="用户名不能为空!"></asp:RequiredFieldValidator> <br/> <asp:Button ID="Button1" runat="server" Text="登录"></asp:Button> <hr/> <hr/> <span style="font-size:12px;"></div></span>

 

CompareValidator 验证控件

  页面布局:

<div>
        <h1>CompareValidator 验证控件</h1>
          价格:
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
         
        <asp:CompareValidator ID="CompareValidator" runat="server" ControlToValidate="TextBox2" 
            ErrorMessage="输入大于0的数值" Operator="GreaterThan" Type="Double" ValueToCompare="0">
        </asp:CompareValidator>
        <asp:Button ID="Button2" runat="server" Text="提交"/>
        <hr/>
        <hr/>
</div>

 

RangeValidator 控件

  页面布局:

<div>  
        <h1>RangeValidator 控件</h1>  
          价格:  
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>  
        <asp:RangeValidator ID="RangeValidator" runat="server" ControlToValidate="TextBox3"   
            ErrorMessage="请输入300~900之间的数" MaximumValue="900" MinimumValue="300" Type="Double" >  
        </asp:RangeValidator>  
</div>  

 

RegularExpressionValidator控件

  页面布局:

<div>
        <h1>RegularExpressionValidator 控件</h1>
          电话号码:
        <asp:TextBox ID="TextBox4" runat="server"/>
        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox4" 
            Display="Dynamic" ErrorMessage="输入合格电话号码如423-875416952" ValidationExpression="(\(\d{3}\)|\d{3}-)?\d{8}"/>
        <asp:Button ID="Button3" runat="server" Text="提交"/>
</div>

 

CustomValidator控件

  页面布局:

<div>
        <h1>CustomValidator控件</h1>
        <table>
            <tr>
                <td style="width:100px" align="right">用户名</td>
                <td style="width:100px">
                    <asp:TextBox ID="TextBox5" runat="server"/>
                </td>
            </tr>
            <tr>
                <td style="width:100px" align="right">密码</td>
                <td style="width :100px">
                    <asp:TextBox ID="TextBox6" runat="server"/>
                </td>
            </tr>
            <tr>
                <td style="width:100px">
                </td>
                <td style="width:100px">
                <asp:Button ID="Button4" runat="server" Text="提交"/>
                </td>
            </tr>
            <tr>
                <td style="width:100px"/>
                <td style="width:150px">
                    <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="用户名或密码错误"
                        ValidateEmptyText="true" ControlToValidate="TextBox6" OnServerValidate="CustomValidator1_ServerValidate"
                        DisPlay="Dynamic">
                    </asp:CustomValidator>
                </td>
            </tr>
        </table>
    </div>

  后台代码:

 //自定义验证函数
        private bool IsPassed(string userName, string password)
        {
            if (userName == "zhouzhou" && password == "123456")
                return true;
            else
                return false;
        }

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (IsPassed(this.TextBox5.Text.ToString(), this.TextBox6.Text.ToString()))//调用自定义函数
                args.IsValid = true;//通过验证
            else
                args.IsValid = false;//未通过验证
        }

 

原文链接:

http://blog.csdn.net/spilledlight/article/details/48730647

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:ASP.NET内置对象二

下一篇:Rich控件一