views:

2527

answers:

6

Hi,

I've got a web form with Password and Confirm Password text boxes. I've got a RegularExpressionValidator attached to the first one and CompareValidator to the second one. Now the problem is when i have something in the Password field and nothing in Confirm Password field it does not display an error that fields don't match. As soon as i put something in the Confirm Password field it shows the error. I also want to allow to leave both fields blank.

I'm using .NET 2.0

What might it be?

+4  A: 

You also need to use a RequiredFieldValidator. A lot of the validation controls will pass if the field is empty and need to be paired in this way with RequiredFieldValidator.

Frank Schwieterman
Thanks to your response, Frank. A clarification to my question. I also want to allow to leave both fields blank. This means i can't use a RequiredValidator on Confirm Password field.
Muxa
+2  A: 

FWIW, if you make the Password box the ControlToValidate, and the Confirm Password box the ControlToCompare, then it will work because the password box will have something in it and hence will run the validation.

Of course, this could allow them to submit a form with an empty Password box and a filled-in Confirm box, so putting a required validator on both is probably a better idea.

patmortech
A: 

CompareValidator, RegularExpressionValidator and RangeValidator validation controls are working on non-empty string values. This is useful for situations where we have an non-required field that needs to satisfy some condition when entered.

For example we have a form with two fields: primary email that must be entered; and alternative email that is not required but when entered it must be validated. To validate this we would add RequiredFieldValidator and RegularExpressionValidator to the primary email and only RegularExpressionValidator to the second field.

It would be tricky to validate mentioned form if RegularExpressionValidator was triggered on empty input, and we would have to change the regex in the second one to allow empty value which is considerably harder to do and mantain and not so obvious solution.

Aleksandar
A: 

A clarification to my question. I also want to allow to leave both fields blank. This means i can't use a RequiredValidator on Confirm Password field.

Muxa
With information updates like this you ought to update question, so that all the relevant information is there.
Gavin Miller
+2  A: 

How about this?

Two TextBox fields - txtEmail1 (for email address) and txtEmail2 (for confirmation).

Attach a RegularExpressionValidator to txtEmail1 -- when blank, it won't fire. when populated, your data is validated.

Attach a CompareValidator to txtEmail1, comparing its data to txtEmail2. Then, attach a CompareValidator to txtEmail2, comparing its data to txtEmail1.

  • When both fields are blank, none of the three validators will fire.
  • When txtEmail1 is populated, it must match the regex, and it must match txtEmail2
  • When txtEmail2 is populated, it must match txtEmail1

This meets your requirement that the fields can be left blank, but fires the validation logic if either field has data.

-- joe

groundh0g
+2  A: 

I had the exact same problem. Use a CustomValidator instead of the CompareValidator. (The CustomValidator has a helpful attribute called ValidateEmptyText, which the CompareValidator lacks, at least in ASP.NET 2.0.)

You will need to program an appropriate ServerValidate function, as well as the ClientValidationFunction. The function signature for the javascript function is basically the same as for the ServerValidate function: source (object), args (ServerValidateEventArgs).

The trickiest part is that you will need to write custom code to access the "CompareTo" textbox, since that's not part of the CustomValidator. My fields were within a FormView; you may need to adjust the code to fit your particular circumstances. In the code below, "fv" is the name of that FormView.

Client-side validation:

<script type="text/javascript">
<!--
  function cvPasswordRpt_Validate(source, args)
  {
    args.IsValid = (args.Value ==
                    document.getElementsByName("fv$tbPassword").item(0).value);
  }
//-->
</script>

ASPX Code:

<label>New Password:</label>
<asp:TextBox ID="tbPassword" runat="server" CssClass="stdTextField" 
             TextMode="Password" ValidationGroup="edit" />
<br />
<label>Repeat New Password:</label>
<asp:TextBox ID="tbPasswordRpt" runat="server" CssClass="stdTextField"
             TextMode="Password" ValidationGroup="edit" />
<asp:CustomValidator ID="cvPasswordRpt" runat="server" Display="Dynamic"
             EnableClientScript="true" ValidationGroup="edit"
             ControlToValidate="tbPasswordRpt" ValidateEmptyText="true"
             ErrorMessage="Your passwords do not match."
             ClientValidationFunction="cvPasswordRpt_Validate"
             OnServerValidate="cvPasswordRpt_ServerValidate" />

Server-side validation (VB.NET):

Protected Sub cvPasswordRpt_ServerValidate(ByVal sender As Object, 
                                           ByVal e As ServerValidateEventArgs)
  Dim _newPassword As String = DirectCast(fv.FindControl("tbPassword"), 
                                          TextBox).Text
  e.IsValid = e.Value.Equals(_newPassword)
End Sub
David
I was having exactly the same problem and this example allowed me to solve it without reading through the lame documentation on MSDN, cheers!
daddywoodland