<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ASP.Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <script> function validateLength(oSrc, args) { args.IsValid = (args.Value.length >= 8); //如果args.IsValid=false,那么服务器将不会执行button的任何操作. } </script> </head> <body> <form id="form1" runat="server"> <div> <h5>登录</h5> RequiredFieldValidator:非空验证 <hr /> <br /> 重要的属性:<br /> 1,ControlToValidate:要验证的控件<br /> 2,ErrorMessage:错误提示信息<br /> <hr /> 用户账号:<asp:TextBox ID="txtUsername" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtUsername" ErrorMessage="请输入用户名"></asp:RequiredFieldValidator> <br /> 密 码:<asp:TextBox ID="txtUserpass" runat="server" TextMode="Password"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtUserpass" ErrorMessage="请输入密码"></asp:RequiredFieldValidator> <br /> <asp:Button ID="btnSubmit" runat="server" Text="登录" /> <br /> </div> <div> <h2>CompareValidator:比较验证</h2> <hr /> 属性:<br /> ControlToValidate:要验证的控件ID<br /> ErrorMessage:错误提示信息<br /> ControlToCompare:与此相比的控件ID<br /> Operator:操作类型<br /> <pre> 1,Equal:相等 2,NotEaula:不相等 3,DataTypeCheck:数据类型验证 4,GreaterThan:大于 5,GreaterThanEqual:大于等于 6,LessThan:小于 7,LessThanEqual:小于等于 </pre> Type:类型<br /> <pre> Integer【整数型】;Date【日期】;Double【双精度浮点型】;String【字符串型】;Currency[货币类型] </pre> ValueToCompare:比较值 <hr /> <br /> Operator="Equal" Type="String"<br /> 1,用户注册时,<br /> 密 码:<asp:TextBox ID="txtUserpass2" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtUserpass2" ErrorMessage="请输入密码"></asp:RequiredFieldValidator> <br /> 重复密码:<asp:TextBox ID="txtUserpass3" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txtUserpass3" Display="Dynamic" ErrorMessage="请输入重复密码"></asp:RequiredFieldValidator> <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="txtUserpass2" ControlToValidate="txtUserpass3" ErrorMessage="重复密码有误" Operator="Equal" ></asp:CompareValidator> <br /> <br /> <br /> Operator="GeaterThan" Type="Currency"<br /> 2,请输入您应付金额:<asp:TextBox ID="txtMoney" runat="server"></asp:TextBox> <asp:CompareValidator ID="CompareValidator2" runat="server" ControlToValidate="txtMoney" ErrorMessage="您输入的金额有误" Operator="GreaterThan" Type="Currency" ValueToCompare="100"></asp:CompareValidator> <br /> <input id="Reset1" type="reset" value="重置" /> <asp:Button ID="Button1" runat="server" Text="提交" /> <br /> </div> <div> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:CustomValidator ID="csd" runat ="server" ClientValidationFunction ="validateLength" ErrorMessage="长度必须是8位" ControlToValidate="TextBox1"></asp:CustomValidator> <asp:Button ID="Button2" runat="server" Text="Button" /> </div> </form> </body> </html>