Hello, everyone today we will see compare validator. So, for that we will discuss

  1. firstly what is the compare validator?
  2. When we have to use compare validator?
  3. What properties do we need to set for the compare validator?
  4. Practically solution.

In previously we already discuss the “required field validation ” & “Range validator ” with full description with practical solution checked it out

1. what is the compare validator?

The CompareValidator control enables you to perform three different
types of validation tasks.

You can use the CompareValidator to perform a data type check.

In other words, you can use the control to determine whether a user
has entered the proper type of value into a form field, such as a date in
a birth date field.

You also can use the CompareValidator to compare the value entered
into a form field against a fixed value.

you can use the CompareValidator to compare the value of one form
field against another.

For example, you use the CompareValidator to check whether the
value entered into the meeting start date is less than the value entered
into the meeting end date.

What properties do we need to set for the compare validator?

The Compare Validator has six important properties:

ControlToValidate—The ID of the form field being validated.

Text—The error message displayed when validation fails.

Type—The type of value being compared. Possible values are
String, Integer, Double, Date, and Currency

Operator—The type of comparison to perform. Possible values
are DataTypeCheck, Equal, GreaterThan, GreaterThanEqual,
LessThan, LessThanEqual, and NotEqual.

ValueToCompare—The fixed value against which to compare.

ControlToCompare—The ID of a control against which to

Practically solution.

Firstly you need to install visual studio and open it.

In this practical, we have to create two text boxes. The first one is for the password field and the second one is for the re-password. So, the user needs to select the same password for the two boxes. Else it gives an error. So, let’s get started.

  1. Firstly We create two fields for password and repassword. For that we drag and drop textbox.
  2. In previous validator we already created name field, age field, and submit button. Meanwhile we create here the same thing.

3. Select compare to validate drag and drop it near the field.

4. Now we need to set the properties of compare validate.
5. Firstly we set the ControlToCompare properties- Select the textbox3.
6. Secondly we set the ControlToValidate properties- Select the textbox4.

7. Write a error message what you want to show the user. Here I am using password not matched.
8. Select the color whatever you want. Here I select red color as usual.

9. Now time to run the program.
10. Firstly you need to check the validation is works properly or not for that you have to write a different word into the two fields.
11. Boooooooooooooooooooooooom here is the error message Password not matched

So, now we test for the correct one.

12. For that firstly you have to write same word into the two fields.


By Tanmay

Leave a Reply

Your email address will not be published. Required fields are marked *