UpdatePanel 内的 CompareValidator - VS2008

发布于 2024-07-08 05:52:04 字数 1239 浏览 5 评论 0原文

我正在使用 UpdatePanel,并希望在两个文本框中放置一个 CompareValidator,以验证用户输入的密码和确认是否相同。

这工作正常(我有 VS2008 并且正在使用 .NET 3.5),开箱即用,有一个小问题:

一旦用户单击第一个文本框,在他们有机会输入内容之前,验证就会触发。第二。 这不会以编程方式造成任何真正的问题(所发生的只是显示错误消息,当他们输入确认信息时它就会消失),但我们的测试人员说这是一个问题。 在单击“保存”之前不会触发验证之前,它不会通过 UA 测试。

如何让 CompareValidator 在他们在两个框中输入文本之前不触发?

编辑:

这是标记的示例。

    <div>
        <div><asp:Label runat="server" ID="lblPassword" Text="Password"/></div>
        <div><asp:TextBox runat="server" TextMode="password" ID="txtPassword" size="25" /></div>    
    </div>
    <div>
        <div><asp:Label runat="server" ID="lblConfirmPassword" Text="Confirm Password"/></div>
        <div><asp:TextBox runat="server" TextMode="password" ID="txtConfirmPassword" size="25" /></div>
    </div>
    <asp:CompareValidator ID="CompareValidator1" ValidationGroup="PublishPassValidation" ControlToValidate="txtPassword" ControlToCompare="txtConfirmPassword" runat="server" ErrorMessage="Passwords do not match"></asp:CompareValidator>

上述内容位于页面上 UpdatePanel 的 ContentTemplate 中包含的控件内。

(为简洁起见,删除了 CSS 类和样式)

I'm using an UpdatePanel and want to put a CompareValidator on two text boxes, to verify that the user-entered password and confirmation are the same.

This is working fine (I have VS2008 and am using .NET 3.5) out of the box, with one minor problem:

The validation is firing as soon as the user clicks out of the first textbox, before they get a chance to type into the second. This isn't causing any REAL problems, programmatically (all that happens is the error message shows, it goes away when they type in the confirmation) but our testers say it is a problem. It won't pass UA testing until the validation doesn't fire until they click 'Save'.

How do I get the CompareValidator to not fire until they've enterred text into both boxes?

EDIT:

Here's an example of the markup.

    <div>
        <div><asp:Label runat="server" ID="lblPassword" Text="Password"/></div>
        <div><asp:TextBox runat="server" TextMode="password" ID="txtPassword" size="25" /></div>    
    </div>
    <div>
        <div><asp:Label runat="server" ID="lblConfirmPassword" Text="Confirm Password"/></div>
        <div><asp:TextBox runat="server" TextMode="password" ID="txtConfirmPassword" size="25" /></div>
    </div>
    <asp:CompareValidator ID="CompareValidator1" ValidationGroup="PublishPassValidation" ControlToValidate="txtPassword" ControlToCompare="txtConfirmPassword" runat="server" ErrorMessage="Passwords do not match"></asp:CompareValidator>

The above is within a control contained within the ContentTemplate of an UpdatePanel on a page.

(CSS classes and styles removed for brevity)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

自由范儿 2024-07-15 05:52:04

尝试切换它,以便在确认文本框而不是密码文本框上完成验证。 这样,在您修改确认文本框或提交表单之前,它不会触发。 您可能希望在密码文本框中有一个必填字段验证器。

<div>
    <div><asp:Label runat="server" ID="lblPassword" Text="Password"/></div>
    <div><asp:TextBox runat="server" TextMode="password" ID="txtPassword" size="25" />
         <asp:RequiredFieldValidator runat="server" ID="passwordRequiredValidator"
                                     ControlToValidate="txtPassword"
                                     ValidationGroup="PublishPassValidation"
                                     ErrorMessage="Password is required."  />    
    </div>    
</div>
<div>
    <div><asp:Label runat="server" ID="lblConfirmPassword" Text="Confirm Password"/></div>
    <div><asp:TextBox runat="server" TextMode="password" ID="txtConfirmPassword" size="25" /></div>
</div>
<asp:CompareValidator ID="CompareValidator1" ValidationGroup="PublishPassValidation"
                      ControlToValidate="txtConfirmPassword"
                      ControlToCompare="txPassword" runat="server"
                      ErrorMessage="Passwords do not match">
</asp:CompareValidator>

Try switching it so that the validation is done on the confirmation text box rather than on the password text box. This way it won't fire until you modify the confirmation text box or the form is submitted. And you probably want to have a required field validator on the password text box.

<div>
    <div><asp:Label runat="server" ID="lblPassword" Text="Password"/></div>
    <div><asp:TextBox runat="server" TextMode="password" ID="txtPassword" size="25" />
         <asp:RequiredFieldValidator runat="server" ID="passwordRequiredValidator"
                                     ControlToValidate="txtPassword"
                                     ValidationGroup="PublishPassValidation"
                                     ErrorMessage="Password is required."  />    
    </div>    
</div>
<div>
    <div><asp:Label runat="server" ID="lblConfirmPassword" Text="Confirm Password"/></div>
    <div><asp:TextBox runat="server" TextMode="password" ID="txtConfirmPassword" size="25" /></div>
</div>
<asp:CompareValidator ID="CompareValidator1" ValidationGroup="PublishPassValidation"
                      ControlToValidate="txtConfirmPassword"
                      ControlToCompare="txPassword" runat="server"
                      ErrorMessage="Passwords do not match">
</asp:CompareValidator>
痞味浪人 2024-07-15 05:52:04

您可以关闭该验证器的客户端验证。

EnableClientScript="false"

不过,这意味着需要往返服务器来报告无效状态,并且您必须确保在继续之前检查页面确实有效。

Page.Validate("PublishPassValidation");

if (Page.IsValid)
{
    // Do Stuff
}

You could turn off client-side validation for that Validator.

EnableClientScript="false"

This would mean a round-trip to the server to report an invalid state, though, and you'd have to ensure that you are checking that the page is indeed valid before continuing.

Page.Validate("PublishPassValidation");

if (Page.IsValid)
{
    // Do Stuff
}
谜泪 2024-07-15 05:52:04

我有一种感觉,您在更新面板上启用了孩子作为触发器?

用户是否在密码框中按“ENTER”? 您能否确认是否由于某种原因更新面板在移动焦点后执行部分刷新?

如果是这样,它将触发验证。

I have a feeling that you have children as triggers enabled on the update panel?

Are the users pressing "ENTER" in the password box? Can you confirm if for some reason the update panel is performing a partial refresh after moving focus?

If so, it would trigger the validation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文