验证摘要未在正确位置显示错误
Validation Summary Not Displaying Errors In Proper Place
我的登录页面中有两个组,login
和 createUser
。我有两个验证摘要,loginSum
和 createSum
。我对每个控件都进行了必填字段验证。如果我在必填字段没有值的情况下单击 Login
或 Create
按钮,错误将出现在该字段下方,而不是在摘要中。
登录码:
<div id="login" class="settings">
asp:Label ID="lblInvalid" runat="server" Visible="false" Display="Static">
YOUR EMAIL OR PASSWORD WERE NOT FOUND. PLEASE REVIEW YOUR INFORMATION AND TRY AGAIN.<br /></asp:Label>
<asp:Label ID="Label1" runat="server" Text="Email:" Width="100px" />
<asp:TextBox ID="user" runat="server" /><br />
<%-- validation for user login email --%>
<asp:RegularExpressionValidator ID="validEmail"
ControlToValidate="user"
ValidationExpression="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?"
Display="Static"
ValidationGroup="login"
ErrorMessage="Email"
SetFocusOnError="true"
runat="server"
Text="*" /><br />
<asp:Label ID="Label2" runat="server" Text="Password:" Width="100px" />
<asp:TextBox ID="password" TextMode="password" runat="server" /><br />
<%-- validation for user login password --%>
<asp:RequiredFieldValidator ID="passReq"
runat="server"
ControlToValidate="password"
ValidationGroup="login"
ErrorMessage="Password"
SetFocusOnError="true" Display="Static"
Text="*" /><br />
<asp:Button ID="submit" runat="server" Text="Login" OnClick="login_Click"
ValidationGroup="login" CausesValidation="true" /><br />
<asp:ValidationSummary ID="loginSum"
DisplayMode="BulletList"
EnableClientScript="true"
ShowSummary="true"
HeaderText="You must enter a valid value for the following fields:"
runat="server" />
</div>
创建代码:
<div id="create_account" class="settings">
<asp:Label ID="Label10" runat="server" Text="Name:" Width="100px" />
<asp:TextBox ID="txtName" runat="server" /><br />
<%-- validation for new user --%>
<asp:RequiredFieldValidator ID="nameReq"
runat="server"
ControlToValidate="txtName"
ValidationGroup="createUser"
ErrorMessage="You must put in a name!"
SetFocusOnError="true" Display="Static"
Text="*" /><br />
<asp:Label ID="Label3" runat="server" Text="Email Address:" Width="100px" />
<asp:TextBox ID="txtNewUser" runat="server" /><br />
<%-- validation for new email --%>
<asp:RequiredFieldValidator ID="newUserReq"
runat="server"
ControlToValidate="txtNewUser"
ValidationExpression="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?"
ValidationGroup="createUser"
ErrorMessage="Email address"
SetFocusOnError="true" Display="Static"
Text="*" /><br />
<asp:Label ID="Label4" runat="server" Text="Password:" Width="100px" />
<asp:TextBox ID="txtNewPass" TextMode="password" runat="server" /><br />
<%-- validation for new password --%>
<asp:RequiredFieldValidator ID="newPassReq"
runat="server"
ControlToValidate="txtNewPass"
ValidationGroup="createUser"
ErrorMessage="Password"
SetFocusOnError="true" Display="Static"
Text="*" /><br />
<asp:Label ID="Label5" runat="server" Text="Confirm Password:" Width="100px" />
<asp:TextBox ID="txtConfirmPass" TextMode="password" runat="server" /><br />
<%-- validation for confirm password --%>
<asp:RequiredFieldValidator ID="confirmPassReq"
runat="server"
ControlToValidate="txtConfirmPass"
ValidationGroup="createUser"
ErrorMessage="Confirm password"
SetFocusOnError="true" Display="Static"
Text="*" /><br />
<asp:CompareValidator ID="ComparePass" runat="server"
ControlToValidate="txtConfirmPass"
CssClass="ValidationError"
ControlToCompare="txtNewPass"
ErrorMessage="The passwords you entered must match" /><br />
<asp:Label ID="label" Text="Address:" runat="server" Width="100px" />
<asp:TextBox ID="txtAddress" TextMode="multiline" Columns="22" Rows="3" runat="server" /><br />
<%-- validation for new user --%>
<asp:RequiredFieldValidator ID="addressReq"
runat="server"
ControlToValidate="txtAddress"
ValidationGroup="createUser"
ErrorMessage="Address"
SetFocusOnError="true" Display="Static"
Text="*" /><br />
<asp:Label ID="label6" Text="City:" runat="server" Width="100px" />
<asp:TextBox ID="txtCity" runat="server" /><br />
<%-- validation for new user --%>
<asp:RequiredFieldValidator ID="cityReq"
runat="server"
ControlToValidate="txtCity"
ValidationGroup="createUser"
ErrorMessage="City"
SetFocusOnError="true" Display="Static"
Text="*" /><br />
<asp:Label ID="label7" Text="State:" runat="server" Width="100px" />
<asp:TextBox ID="txtState" runat="server" /><br />
<%-- validation for new user --%>
<asp:RequiredFieldValidator ID="stateReq"
runat="server"
ControlToValidate="txtState"
ValidationGroup="createUser"
ErrorMessage="State (abbreviation)"
ValidationExpression="[a-zA-Z]{2}"
SetFocusOnError="true" Display="Static"
Text="*" /><br />
<asp:Label ID="label8" Text="Zip Code:" runat="server" Width="100px" />
<asp:TextBox ID="txtZip" runat="server" /><br />
<%-- validation for new user --%>
<asp:RequiredFieldValidator ID="zipReq"
runat="server"
ControlToValidate="txtZip"
ValidationGroup="createUser"
ErrorMessage="Zip code"
ValidationExpression="[a-zA-Z]{9}"
SetFocusOnError="true" Display="Static"
Text="*" /><br />
<asp:Label ID="label9" Text="Phone Number:" runat="server" Width="100px" />
<asp:TextBox ID="txtPhone" runat="server" /><br />
<%-- validation for new user --%>
<asp:RequiredFieldValidator ID="phoneReq"
runat="server"
ControlToValidate="txtPhone"
ValidationGroup="createUser"
ErrorMessage="Phone number"
ValidationExpression="[a-zA-Z]{15}"
SetFocusOnError="true" Display="Static"
Text="*" /><br />
<asp:Button ID="create" runat="server"
Text="Create Account" OnClick="create_Click"
ValidationGroup="createUser" CausesValidation="true" /><br />
<br />
<asp:ValidationSummary ID="createSum"
DisplayMode="BulletList"
ShowSummary="true"
EnableClientScript="true"
HeaderText="You must enter a valid value for the following fields:"
runat="server" />
</div>
任何人都可以帮助我或指导我找到有效的教程吗?我现在得到的只是所有内容下面的“*”,如果我去掉 Text="*"
,那么它只会在每个关联控件下面显示 ErrorMessage="_____"
,而不是像它应该的那样显示在验证摘要中。
您需要将验证组属性添加到所有验证器和两个摘要,以及按钮(每个按钮只会验证一个组)即
ValidationGroup="logingroup"
ValidationGroup="creategroup"
请看(^)
我的登录页面中有两个组,login
和 createUser
。我有两个验证摘要,loginSum
和 createSum
。我对每个控件都进行了必填字段验证。如果我在必填字段没有值的情况下单击 Login
或 Create
按钮,错误将出现在该字段下方,而不是在摘要中。
登录码:
<div id="login" class="settings">
asp:Label ID="lblInvalid" runat="server" Visible="false" Display="Static">
YOUR EMAIL OR PASSWORD WERE NOT FOUND. PLEASE REVIEW YOUR INFORMATION AND TRY AGAIN.<br /></asp:Label>
<asp:Label ID="Label1" runat="server" Text="Email:" Width="100px" />
<asp:TextBox ID="user" runat="server" /><br />
<%-- validation for user login email --%>
<asp:RegularExpressionValidator ID="validEmail"
ControlToValidate="user"
ValidationExpression="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?"
Display="Static"
ValidationGroup="login"
ErrorMessage="Email"
SetFocusOnError="true"
runat="server"
Text="*" /><br />
<asp:Label ID="Label2" runat="server" Text="Password:" Width="100px" />
<asp:TextBox ID="password" TextMode="password" runat="server" /><br />
<%-- validation for user login password --%>
<asp:RequiredFieldValidator ID="passReq"
runat="server"
ControlToValidate="password"
ValidationGroup="login"
ErrorMessage="Password"
SetFocusOnError="true" Display="Static"
Text="*" /><br />
<asp:Button ID="submit" runat="server" Text="Login" OnClick="login_Click"
ValidationGroup="login" CausesValidation="true" /><br />
<asp:ValidationSummary ID="loginSum"
DisplayMode="BulletList"
EnableClientScript="true"
ShowSummary="true"
HeaderText="You must enter a valid value for the following fields:"
runat="server" />
</div>
创建代码:
<div id="create_account" class="settings">
<asp:Label ID="Label10" runat="server" Text="Name:" Width="100px" />
<asp:TextBox ID="txtName" runat="server" /><br />
<%-- validation for new user --%>
<asp:RequiredFieldValidator ID="nameReq"
runat="server"
ControlToValidate="txtName"
ValidationGroup="createUser"
ErrorMessage="You must put in a name!"
SetFocusOnError="true" Display="Static"
Text="*" /><br />
<asp:Label ID="Label3" runat="server" Text="Email Address:" Width="100px" />
<asp:TextBox ID="txtNewUser" runat="server" /><br />
<%-- validation for new email --%>
<asp:RequiredFieldValidator ID="newUserReq"
runat="server"
ControlToValidate="txtNewUser"
ValidationExpression="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?"
ValidationGroup="createUser"
ErrorMessage="Email address"
SetFocusOnError="true" Display="Static"
Text="*" /><br />
<asp:Label ID="Label4" runat="server" Text="Password:" Width="100px" />
<asp:TextBox ID="txtNewPass" TextMode="password" runat="server" /><br />
<%-- validation for new password --%>
<asp:RequiredFieldValidator ID="newPassReq"
runat="server"
ControlToValidate="txtNewPass"
ValidationGroup="createUser"
ErrorMessage="Password"
SetFocusOnError="true" Display="Static"
Text="*" /><br />
<asp:Label ID="Label5" runat="server" Text="Confirm Password:" Width="100px" />
<asp:TextBox ID="txtConfirmPass" TextMode="password" runat="server" /><br />
<%-- validation for confirm password --%>
<asp:RequiredFieldValidator ID="confirmPassReq"
runat="server"
ControlToValidate="txtConfirmPass"
ValidationGroup="createUser"
ErrorMessage="Confirm password"
SetFocusOnError="true" Display="Static"
Text="*" /><br />
<asp:CompareValidator ID="ComparePass" runat="server"
ControlToValidate="txtConfirmPass"
CssClass="ValidationError"
ControlToCompare="txtNewPass"
ErrorMessage="The passwords you entered must match" /><br />
<asp:Label ID="label" Text="Address:" runat="server" Width="100px" />
<asp:TextBox ID="txtAddress" TextMode="multiline" Columns="22" Rows="3" runat="server" /><br />
<%-- validation for new user --%>
<asp:RequiredFieldValidator ID="addressReq"
runat="server"
ControlToValidate="txtAddress"
ValidationGroup="createUser"
ErrorMessage="Address"
SetFocusOnError="true" Display="Static"
Text="*" /><br />
<asp:Label ID="label6" Text="City:" runat="server" Width="100px" />
<asp:TextBox ID="txtCity" runat="server" /><br />
<%-- validation for new user --%>
<asp:RequiredFieldValidator ID="cityReq"
runat="server"
ControlToValidate="txtCity"
ValidationGroup="createUser"
ErrorMessage="City"
SetFocusOnError="true" Display="Static"
Text="*" /><br />
<asp:Label ID="label7" Text="State:" runat="server" Width="100px" />
<asp:TextBox ID="txtState" runat="server" /><br />
<%-- validation for new user --%>
<asp:RequiredFieldValidator ID="stateReq"
runat="server"
ControlToValidate="txtState"
ValidationGroup="createUser"
ErrorMessage="State (abbreviation)"
ValidationExpression="[a-zA-Z]{2}"
SetFocusOnError="true" Display="Static"
Text="*" /><br />
<asp:Label ID="label8" Text="Zip Code:" runat="server" Width="100px" />
<asp:TextBox ID="txtZip" runat="server" /><br />
<%-- validation for new user --%>
<asp:RequiredFieldValidator ID="zipReq"
runat="server"
ControlToValidate="txtZip"
ValidationGroup="createUser"
ErrorMessage="Zip code"
ValidationExpression="[a-zA-Z]{9}"
SetFocusOnError="true" Display="Static"
Text="*" /><br />
<asp:Label ID="label9" Text="Phone Number:" runat="server" Width="100px" />
<asp:TextBox ID="txtPhone" runat="server" /><br />
<%-- validation for new user --%>
<asp:RequiredFieldValidator ID="phoneReq"
runat="server"
ControlToValidate="txtPhone"
ValidationGroup="createUser"
ErrorMessage="Phone number"
ValidationExpression="[a-zA-Z]{15}"
SetFocusOnError="true" Display="Static"
Text="*" /><br />
<asp:Button ID="create" runat="server"
Text="Create Account" OnClick="create_Click"
ValidationGroup="createUser" CausesValidation="true" /><br />
<br />
<asp:ValidationSummary ID="createSum"
DisplayMode="BulletList"
ShowSummary="true"
EnableClientScript="true"
HeaderText="You must enter a valid value for the following fields:"
runat="server" />
</div>
任何人都可以帮助我或指导我找到有效的教程吗?我现在得到的只是所有内容下面的“*”,如果我去掉 Text="*"
,那么它只会在每个关联控件下面显示 ErrorMessage="_____"
,而不是像它应该的那样显示在验证摘要中。
您需要将验证组属性添加到所有验证器和两个摘要,以及按钮(每个按钮只会验证一个组)即
ValidationGroup="logingroup"
ValidationGroup="creategroup"
请看(^)