---
title: "CompareValidator Control in ASP.Net"  
description: "The CompareValidator control is used to compare the value of one input control to the value of another input control or to a fixed value.  ErrorMessag"  
author: "Pushpendra Singh"  
published: 2010-11-10  
updated: 2020-03-04  
canonical: https://www.mindstick.com/articles/198/comparevalidator-control-in-asp-dot-net  
category: "asp.net"  
tags: ["asp.net"]  
reading_time: 2 minutes  

---

# CompareValidator Control in ASP.Net

The [CompareValidator](https://www.mindstick.com/forum/33588/how-to-use-comparevalidator-in-asp-dot-net) [control](https://yourviews.mindstick.com/view/83439/bipartisan-gun-control-bill-passed-in-us-after-decades) is used to compare the value of one input control to the value of another input control or to a fixed value.\

```
<asp:CompareValidator ID="CompareValidator1" runat="server"  ErrorMessage="CompareValidator"></asp:CompareValidator>
```

## \

**[Properties](https://yourviews.mindstick.com/view/81845/now-it-s-possible-to-predict-properties-of-any-molecule):**\

| ControlToCompare | The name of the control to compare with |
| --- | --- |
| ControlToValidate | The id of the control to [validate](https://www.mindstick.com/forum/1490/what-is-better-solution-for-validate-form-textboxs-value) |
| [Error Message](https://www.mindstick.com/forum/23174/error-message-the-page-you-are-requesting-cannot-be-served-because-of-the-extension-configuration) | The text to display in the page when [validation fails](https://www.mindstick.com/forum/34291/how-to-raise-a-callback-when-asp-dot-net-mvc-client-validation-fails). Note: This text will also be displayed in the validation control if the [Text property](https://www.mindstick.com/forum/161/problem-in-getting-text-property-for-sender-object) is not set |
| ValueToCompare | A specified value to compare with |

## Code:

```
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><asp:Button ID="Button1" runat="server" Text="Submit" /><asp:CompareValidator ID="CompareValidator1" runat="server"             ControlToCompare="TextBox1" ControlToValidate="TextBox2"             ErrorMessage="Not Matched"></asp:CompareValidator>
```

When we will not enter same value in both TextBox then error message will be displayed.

![CompareValidator Control in ASP.Net](https://www.mindstick.com/mindstickarticle/707f69de-fb81-41b5-bd32-62dbc857ea66/images/b91033b7-4253-4e05-ae00-a064e3da7bfd.png)

When we enter same value in both TextBox then page will execute

![CompareValidator Control in ASP.Net](https://www.mindstick.com/mindstickarticle/707f69de-fb81-41b5-bd32-62dbc857ea66/images/cb29768d-83c9-4e62-83b6-d695808105d0.png)

To compare with a fixed value you can write the value to compare in ValueToCompareproperty of CompareValidator.

```
<asp:CompareValidator ID="CompareValidator2" runat="server"ControlToValidate="TextBox3" ErrorMessage="Enter your area Code" ValueToCompare="20"></asp:CompareValidator>
```

Here in the above code we are comparing the value of TextBox3 with 20. By default this will check for equal value. You can also check it for greater than or less than by setting the [Operator](https://www.mindstick.com/blog/144/union-intersection-and-except-operator-in-sql-server) property to GreaterThan or LessThan, or what ever you want to as per you requirement.

---

Original Source: https://www.mindstick.com/articles/198/comparevalidator-control-in-asp-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
