你的位置:首页 > ASP.net教程

[ASP.net教程]ASP.NET验证控件二


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 ></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 align="right">用户名</td>        <td >          <asp:TextBox ID="TextBox5" runat="server"/>        </td>      </tr>      <tr>        <td align="right">密码</td>        <td >          <asp:TextBox ID="TextBox6" runat="server"/>        </td>      </tr>      <tr>        <td >        </td>        <td >        <asp:Button ID="Button4" runat="server" Text="提交"/>        </td>      </tr>      <tr>        <td />        <td >          <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