CompareValidator is missing values

1.1k Views Asked by At

I have two text box controls, txtPassword and txtPassword2 on a web form.

Using a CompareValidator control, both fields need to match.

txtPassword = "" 
txtPassword2 = "" 
No compare error 
txtPassword throws it's required field error

txtPassword = "1" 
txtPassword2 = "" 
No compare error

txtPassword = "" 
txtPassword2 = "1" 
Compare error 
txtPassword throws it's required field error

txtPassword = "1" 
txtPassword2 = "2" 
Compare error

Any idea why it's missing blank values for txtPassword2?

Here's the code:

<asp:TextBox ID="txtPassword" Text="" TextMode="Password" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Required!" ControlToValidate="txtPassword"></asp:RequiredFieldValidator>

<asp:TextBox ID="txtPassword2" Text="" TextMode="Password" runat="server"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="Passwords do not match!" ControlToValidate="txtPassword2" ControlToCompare="txtPassword"></asp:CompareValidator>
2

There are 2 best solutions below

1
AudioBubble On BEST ANSWER

You are adding asp:RequiredFieldValidator only for txtPassword, so if txtPassword2 left blank will not throw required field error. one thing you need to add is asp:RequiredFieldValidator for txtPassword2, and also you need to specify the validation group. hence the code will be like:

<asp:TextBox ID="txtPassword" Text="" TextMode="Password" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Required!" ControlToValidate="txtPassword" ValidationGroup="checkNull"></asp:RequiredFieldValidator>

<asp:TextBox ID="txtPassword2" Text="" TextMode="Password" runat="server"></asp:TextBox>

<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Required!" ControlToValidate="txtPassword2" ValidationGroup="checkNull"></asp:RequiredFieldValidator>

<asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="Passwords do not match!" ControlToValidate="txtPassword2" ControlToCompare="txtPassword"></asp:CompareValidator>
0
Craig Trunzo On

I had a similar situation where I wanted the password to be required during "insert" and used RequiredFieldValidators to ensure both password boxes were completed with a CompareValidator making sure they matched.

In edit mode, i had the RequiredFieldValidators turned off and ran into the same problem, the PasswordConfirm Textbox didn't stop a match if it was left blank.

I solved this by using 2 CompareValidators as so with the ControlToValidate and ControlToCompare fields swapped. This forced them to validate each other so that if anything was typed in either one, it forced a check

<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="tbPassword" ControlToCompare="tbConfirmPassword"  Type="String" Display="Dynamic" ForeColor="Red" ErrorMessage="<br />The new passwords do not match" />
<asp:CompareValidator ID="CompareValidator2" runat="server" ControlToValidate="tbConfirmPassword" ControlToCompare="tbPassword"  Type="String" Display="Dynamic" ForeColor="Red" ErrorMessage="<br />The new passwords do not match" />