简体   繁体   中英

CompareValidator without RequiredFieldValidator?

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!

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)(?=.*[az])(?=.*[AZ])(?=.*[\\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> 

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM