5

So I've been looking all over and can't seem to find a similar problem.

Basically, it seems like using CompareValidator doesn't work without a RequiredFieldValidator.

<div class="control-group">
                <label class="control-label" for="PositionName">
                    Password:</label>
                <div class="controls">
                    <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
                    <%--<asp:RequiredFieldValidator ID="rvPassword" runat="server" ControlToValidate="txtPassword"
                        ErrorMessage="Please Enter Password" SetFocusOnError="True" ValidationGroup="1"
                        CssClass="error"></asp:RequiredFieldValidator>--%>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="PositionName">
                    Confirm Password:</label>
                <div class="controls">
                    <asp:TextBox ID="txtConfirmPassword" runat="server" TextMode="Password"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="reqConPass" runat="server" ControlToValidate="txtConfirmPassword"
                        ErrorMessage="Please Enter Confirm Password" SetFocusOnError="True" ValidationGroup="1"
                        CssClass="error"></asp:RequiredFieldValidator>
                    <asp:CompareValidator ID="compPassword" runat="server" ControlToValidate="txtConfirmPassword"
                        ControlToCompare="txtPassword" ErrorMessage="Password Mismatch" SetFocusOnError="True"
                        ValidationGroup="1" CssClass="error"></asp:CompareValidator>
                </div>
            </div>

Basically, you can see I have the RequiredFieldValidator commented out for both pass and confirm pass. When I do this, I can submit with only a value in the txtPassword textbox and nothing in the txtConfirmPassword textbox.

If I uncomment the RequiredFieldValidators then it compares as it should.

If it helps, the reason I need to do this is because I am unable to decrypt the password and autofill the textbox with their current password. So whenever a user is editted, they will need to enter a new password everytime with a RequiredFieldValidator on it.

So my solution was to get rid of the RequiredFieldValidator and just check if the text is null or empty, and if it is, don't update the password, but if it isn't then update the user without updating the password.

I hope this makes sense, and if anyone can help I would greatly appreciate it.

If you need more info please ask.

Thanks again!

Fjodr
  • 919
  • 13
  • 32
Andy Giebel
  • 223
  • 1
  • 14
  • Yes, this is by design. You can do the compare check server-side or use a custom validator. – H H May 21 '15 at 16:52
  • This worked for me perfectly. I copied your given code directly and even without the required validator on password I was given "Password Mismatch" - though it did perform a PostBack in my test. – ahwm May 21 '15 at 16:58
  • Hmm interesting that it worked for you ahwm, still doesn't seem to like it for me. Oh well, I suppose I will take Henk Holterman's advise and do that check server side. Thanks guys! – Andy Giebel May 21 '15 at 17:16
  • Additional test note: I even commented the `RequiredFieldValidator` on `txtConfirmPassword` and it worked. So I'm not sure what's interfering with it on yours. – ahwm May 21 '15 at 17:25
  • Ohhh okay, so what happened was the code I pasted didn't have the requiredfieldvalidator commented out for the confirmpassword textbox. This did make the compare work, however it then wouldn't let me submit without entering a password at all, which is the problem I'm trying to fix. – Andy Giebel May 21 '15 at 17:25

2 Answers2

0

See this code snippet, in this first for password i have used Regular expression validator and once password is valid then i have enabled compare validator.

 <script>
        function Validate() {            
            if (document.getElementById('<%=txtPassword.ClientID %>').value != "")
                ValidatorEnable(document.getElementById('<%=ConfirmPasswordRequired.ClientID %>'), true);
            else
                ValidatorEnable(document.getElementById('<%=ConfirmPasswordRequired.ClientID %>'), false);
        }
    </script>
 <p>
              Password
                <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
               
                <asp:RegularExpressionValidator ID="PasswordRegularExpression" runat="server"
                    ErrorMessage="*Password must be at least 8 characters long and include at least one Special Character, one Number, and one Capital letter."
                    ValidationGroup="ValidationGroup1" ValidationExpression="^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\W]).*$"
                    ControlToValidate="txtPassword" >
                </asp:RegularExpressionValidator>
            </p>
            <p>
    Confirm Password:
                <asp:TextBox ID="txtConfirmPassword" runat="server" TextMode="Password"></asp:TextBox>
                <asp:RequiredFieldValidator ID="ConfirmPasswordRequired" runat="server" ControlToValidate="txtConfirmPassword"
                    ErrorMessage="*Confirm Password is required."
                    Enabled="false" ValidationGroup="ValidationGroup1"></asp:RequiredFieldValidator>
                <asp:CompareValidator ID="NewPasswordCompare" runat="server" ControlToCompare="txtPassword"
                    ControlToValidate="txtConfirmPassword"  ErrorMessage="*Confirm Password must match with the Password."
                    ValidationGroup="ValidationGroup1"></asp:CompareValidator>
            </p>
            <p>
                <asp:Button ID="Button1" runat="server" Text="Save" ValidationGroup="ValidationGroup1"
                    OnClick="Button1_Click" OnClientClick="Validate();" />
            </p>
GMD
  • 761
  • 5
  • 21
0

Here's one thought, I also ended up using this solution:

How about setting the compare validator to validate the password textbox and compare it to the confirmation. This way, the compare validator only fires if there is a value inside the password textbox.

<div class="control-group">
    <label class="control-label" for="PositionName">Password:</label>
    <div class="controls">
        <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"/>      
    </div>
</div>
<div class="control-group">
    <label class="control-label" for="PositionName">Confirm Password:</label>
    <div class="controls">
        <asp:TextBox ID="txtConfirmPassword" runat="server" TextMode="Password"/>       
        <asp:CompareValidator ID="compPassword" runat="server" ControlToValidate="txtPassword"
                      ControlToCompare="txtConfirmPassword" ErrorMessage="Password Mismatch" SetFocusOnError="True"
                      ValidationGroup="1" CssClass="error"/>
    </div>
</div>
Mihai Caracostea
  • 8,336
  • 4
  • 27
  • 46