• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 287
  • Last Modified:

ASP.NET CompareValidator: Verifying that UserID and Password are not the same

I have two text boxes: txtProviderId and txtProviderPassword.  I'm trying to compare the values, verifying that they are not same; user id and password cannot be the same.  I'm using the CompareValidator control and the code is as follows:

<asp:CompareValidator ID="cvProviderIDPswd" runat="server"
        ErrorMessage="Provider password must be different from Provider ID"

When I test my program and add the same value to both textboxes, the CompareValidator control is not detecting that they are not equal and I do not an error message.

Can someone please tell me what I am doing wrong?  Thank you
Carmen Torres
Carmen Torres
1 Solution
What event is validating the textbox values? The validation is usually carried out when a button is clicked, or some other event occurs and then another control (such as a label) will be set to display the error message. Just having the validator in the page is not enough.

Look at the example in the link below. You will see that there is a text box to validate, a button that gets clicked to do the validation and a label that has its text updated to display the error message.

Change your display property to anything other than Display="None"
Carmen TorresData AnalystAuthor Commented:
The event that is validating the textbox is a button.  I only provided the comparevalidator statement, but the statement for the button is as follows:

<asp:Button ID="btnLogin" runat="server" BorderStyle="Solid" Text="Log In" CssClass="Buttons" OnClick="btnLogin_Click" ValidationGroup="login"></asp:Button>

Please note that I'm also using the ValidationSummary Control which is used to display a summary of all validation errors occurred in a Web page.  This control works for all other textboxes but the two that I am comparing.  

Any thoughts?

Thank you.
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Alan WarrenCommented:
Hi CarmenMTorres,

It works fine here, am I missing something?
<%@ Page Language="VB" %>

<!DOCTYPE html>

<script runat="server">


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <form id="form1" runat="server">

      <asp:TextBox ID="txtProviderId" runat="server"></asp:TextBox>
      <asp:TextBox ID="txtProviderPassword" runat="server" TextMode="Password"></asp:TextBox>
      <asp:CompareValidator ID="cv1" runat="server" 
        ErrorMessage="Password and ProviderID cannot be the same!" 
        ValidationGroup="login" Display="None" 
        ControlToCompare="txtProviderId" ControlToValidate="txtProviderPassword" />
      <asp:Button ID="Button1" runat="server" Text="Button" ValidationGroup="login" />
      <asp:ValidationSummary ID="ValidationSummary1" runat="server" ValidationGroup="login" />

Open in new window

Respectfully yours,
Carmen TorresData AnalystAuthor Commented:
Alan,  I noticed that in your code you did not include the statement "Operator = 'Not Equal'".  How are you instructing the program to compare for inequality?
Alan WarrenCommented:
Hi CarmenMTorres,
I must have dropped that when copying or editing the sample code in this editor.

The code I posted would currently be triggering on the default operator value of "Equal"{0} and should be set to Operator = 'Not Equal'{1}.

Respectfully yours,
Carmen TorresData AnalystAuthor Commented:
Thanks Alan.  I have to go back and do some more investigation.  Somehow the code is not detecting that password and userId are the same.

Thanks for you help.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now