How to use ASP.NET CompareValidator Control

In this tutorial you will learn what is and how to use ASP.NET CompareValidator Control. ASP.NET CompareValidator Control is used to compare a value input to another input value or a fixed value. The following are a few things you should keep in mind when using ASP.NET CompareValidator Control:

  • CompareValidator Control can show successful comparison even if the input was empty.
  • It is a good practice to use the ASP.NET RequiredFieldValidator control to make sure the Input control field is a required field.

Adding ASP.NET CompareValidator Control

The following shows you step by step on how to add an ASP.NET CompareValidator Control in Visual Studio 2010.

  1. Open Visual Studio 2010 from the Start menu –> All Programs
  2. Go to the Toolbox and under the Validation section, select CompareValidator.

ASP.NET CompareValidator Control

ASP.NET CompareValidator Control

  1. Drag and drop it on to the Form area.
<asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="CompareValidator"></asp:CompareValidator>
  1. The following is shows an example use of the ASP.NET CompareValidator Control.
<form id="form1" runat="server">
     <p>Comparing Values</p>
     <asp:TextBox id="TextBox1" runat="server" />
     <asp:TextBox id="TextBox2" runat="server" />
     <asp:Button ID="Button2" Text="Validate" runat="server" />
     <br />
     <asp:CompareValidator id="CompareValidator1" Display="dynamic" ControlToValidate="TextBox1" ControlToCompare="TextBox2" ForeColor="red" BackColor="white" Type="String" EnableClientScript="false" Text="Comparision Failed" runat="server" />
  1. In the above code snippet, two textbox controls, a button control named “Validate” and one CompareValidator control was created.
  2. If the validation fails, the validation text “Comparison Failed” will be displayed in Red by the CompareValidator Control.

ASP.NET CompareValidator Control properties

The following table contains some of the important attributes and properties used together with the ASP.NET CompareValidator Control.

“BackColor”Property to set the background color for the Control
“ControlToCompare”Property to set the name of the ASP.NET Control to Compare to
“ControlToValidate”Property to set the id of the ASP.NET Control to validate
“Display”Property to set the display behavior for the ASP.NET validation control.

  • “None”– does not display the validation and only displayed in the ASP.NET ValidationSummary control.
  • “Static”– displays the validation message if check fails. The space of the control is reserved even if the check passes.
  • “Dynamic”– displays the validation message if check fails. The space of the control is not reserved if the check passes.
“EnableClientScript”Property to set a Boolean value to specify if client-side validation is enabled or disabled.
“Enabled”Property to set a Boolean value to specify if validation control is enabled or disabled.
“ErrorMessage”Property to set the Validation text to be displayed in the ValidationSummary Control when the validation check fails. Even if the text property is not set, the default text will be displayed in the validation control.
“ForeColor”To set the foreground color of the ASP.NET CompareValidator Control
“id”A unique ID for the ASP.NET CompareValidator Control
“IsValid”Property to set a Boolean value to indicate if the control specified by ControlToValidate is determined to be valid.
“Operator”To set the type of comparison to perform:

  • “Equal”
  • “GreaterThan”
  • “GreaterThanEqual”
  • “LessThan”
  • “LessThanEqual”
  • “NotEqual”
  • “DataTypeCheck”
“runat”To indicate that the Control is a server control (by setting it to “”server””)
“Text”To set the validation text message to be displayed when check fails.
“Type”To set the data type of the values to compare:

  • “Currency”
  • “Date”
  • “Double”
  • “Integer”
  • “String”
“ValueToCompare”To set the value to compare with.

Referenced from: ASP.NET CompareValidator Control Properties

Related Tutorials:

This entry was posted in ASP.NET Tutorial and tagged , , , . Bookmark the permalink.

One Response to "How to use ASP.NET CompareValidator Control"