This tutorial is the second part of the ASP.NET Validation tutorial series. This tutorial will demonstrate how to use the compare validator for server side validation using ASP.NET 4.0.

What is the CompareValidator?

The compare validator is an ASP.NET control that allows us to compare either two controls, or a control and a static value. The compare validator uses server side validation based on an operator and two values to determine whether input is valid or not. An example of where you could use a compare validator would be if you needed the user to type in something more than once, such as a password. You could then use this to validate that they have the correct password entered into both text boxes.

Using the CompareValidator

To demonstrate the compare validator, we will create a simple web site which will have two text boxes and a button. When the button is clicked, we will use the compare validator to compare the values of the text boxes and determine whether an error needs to be displayed. To do this, create a new ASP.NET Empty Web Site and:

  1. Right click the project in your solution explorer.
  2. Select add new item…
  3. Select a web form.
  4. Name it ‘Default.aspx’.
  5. Click add.
  6. Open Default.aspx up to design mode.
  7. Drag and drop a textbox onto the web form.
  8. Add a break line.
  9. Drag and drop a textbox onto the web form.
  10. Add a break line.
  11. Drag and drop a button onto the web form.
  12. Drag and drop a comparevalidator onto the web form.
  13. Change the ErrorMessage property to ‘The current input is not valid.’
  14. Set the ControlToValidate property to ‘TextBox1’.
  15. Set the ControlToCompare property to ‘TextBox2’.

What this has done is create a simple web page with a compare validator associated with two text boxes on it. Then, we have added a button to trigger the validation. The compare validator uses a type property and operator property to determine how to compare the two controls. The validation attempts to convert the data from the given controls to the type specified to compare the two values using the specified operator. The different types and operators are as follows:

 Types  Operators
 String  DataTypeCheck
 Integer  Equal
 Double  GreaterThan
 Date  GreaterThanEqual
 Currency  LessThan
 LessThanEqual
 NotEqual

Testing

Because we have two text boxes, we will need to test out a few different scenarios to figure out if our validator is working properly. To do this, test the following scenarios by entering in the appropriate data for each text box and clicking the button to determine if the error message is displayed:

 TextBox1  TextBox2  Result
 Empty  Empty  No error, the fields match.
 ‘asdf’  Empty  Error, the fields do not match.
 ‘asdf’  ‘asdf’  No error, the fields match.
 ‘asdf’  ‘asdfasdf’  Error, the fields do not match.
 Empty  ‘asdf’  No error, this is because no error will be thrown when the first data field is empty.

Notice that the last test caused no error even though the text boxes did not contain the same value. The simplest way around this issue is by applying a required field validator to the first text box so that it would be ruled invalid before it got to this point.

The compare validator is an easy way to compare a control’s value to another control or static value. To configure the validator to test against a static value, you simply need to change the ValueToCompare property. However, you will not want to compare a control with both a static value and another control as the second control will take precedence over the static value.
Download Source Files