views:

869

answers:

3

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)

+1  A: 

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
}
Kevin Gorski
+1  A: 

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.

FlySwat
+1  A: 

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>
tvanfosson
RFV's added to both boxes, and I took your advice. Frankly, it's sub-optimal (since the behavior is still present, just much less likely to be seen) but it's a very minor thing and should satisfy the QA testers.
Jeff