练习点:
1.RequiredFiledValidation控件
2.RegularExpressionValidation控件
3.CompareValidator控件
4.ValidationSummary控件
5.复选框动态选择必填项
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ValidationDemo.aspx.cs" Inherits="ValidationDemo" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <asp:ValidationSummary ID="RegisterUserValidationSummary" runat="server" ValidationGroup="RegisterUserValidationGroup"/> <div class="accountInfo"> <fieldset class="register"> <legend>帐户信息</legend> <p> <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">用户名:</asp:Label> <asp:TextBox ID="UserName" runat="server" ></asp:TextBox> <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" ErrorMessage="必须填写“用户名”。" ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator> </p> <p> <asp:Label ID="EmailLabel" runat="server" AssociatedControlID="Email">电子邮件:</asp:Label> <asp:TextBox ID="Email" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="EmailRequired" runat="server" ControlToValidate="Email" ErrorMessage="必须填写“电子邮件”。" ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator> <asp:RegularExpressionValidator ErrorMessage="请正确填写Email" ControlToValidate="Email" runat="server" ValidationGroup="RegisterUserValidationGroup" Display=Dynamic ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" /> </p> <p> <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">密码:</asp:Label> <asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox> <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password" ErrorMessage="必须填写“密码”。" ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator> </p> <p> <asp:Label ID="ConfirmPasswordLabel" runat="server" AssociatedControlID="ConfirmPassword">确认密码:</asp:Label> <asp:TextBox ID="ConfirmPassword" runat="server" TextMode="Password"></asp:TextBox> <asp:RequiredFieldValidator ControlToValidate="ConfirmPassword" Display="Dynamic" ErrorMessage="必须填写“确认密码”。" ID="ConfirmPasswordRequired" runat="server" ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator> <asp:CompareValidator ID="PasswordCompare" runat="server" ControlToCompare="Password" ControlToValidate="ConfirmPassword" Display="Dynamic" ErrorMessage="“密码”和“确认密码”必须匹配。" ValidationGroup="RegisterUserValidationGroup">*</asp:CompareValidator> </p> <p> <asp:CheckBox ID="cb_PhoneNumber" Text="手机:" runat="server" oncheckedchanged="cb_PhoneNumber_CheckedChanged" /> <asp:TextBox runat="server" ID="tb_PhoneNumber" /> <asp:RequiredFieldValidator ID="RequireVphonenumber" Display="Dynamic" ErrorMessage="请填写手机号" ControlToValidate="tb_PhoneNumber" runat="server" /> <asp:RegularExpressionValidator ID="RegularVphonenumber" Display="Dynamic" ErrorMessage="请正确填写" ControlToValidate="tb_PhoneNumber" runat="server" ValidationExpression="\d{6}" /> </p> <p> <asp:CheckBox ID="cb_Number" Text="座机:" runat="server" oncheckedchanged="cb_Number_CheckedChanged" /> <asp:TextBox runat="server" ID="tb_Number" /> <asp:RequiredFieldValidator ID="RequiredVnumber" ErrorMessage="请填写座机号" Display="Dynamic" ControlToValidate="tb_Number" runat="server" /> <asp:RegularExpressionValidator ID="RegularVnumber" ErrorMessage="请正确填写" Display="Dynamic" ControlToValidate="tb_Number" runat="server" ValidationExpression="(\(\d{3}\)|\d{3}-)?\d{8}" /> </p> </fieldset> <p class="submitButton"> <asp:Button ID="CreateUserButton" runat="server" CommandName="MoveNext" Text="创建用户" ValidationGroup="RegisterUserValidationGroup" onclick="CreateUserButton_Click"/> </p> </div> <%-- Submit a Reservation<br /> <asp:ValidationSummary ID="vs" runat="server" /> <asp:Label Text="Email:" runat="server" /> <asp:TextBox ID="tb_Email" runat="server" /> <br /> <asp:Label Text="NumberInParty:" runat="server" /> <asp:TextBox ID="tb_NumberInParty" runat="server" /> <br /> <asp:Label Text="PreferredDate:" runat="server" /> <asp:TextBox ID="tb_PreferredDate" runat="server" /> <br /> <br /> <asp:Button Text="Submit" ID="btn_Submit" runat="server" onclick="btn_Submit_Click" /> <asp:Label Text="" ID="lb_Message" runat="server" />--%> </form> </body> </html>