---
title: "How to use CompareValidator in asp.net"  
description: "How to use CompareValidator in asp.net"  
author: "Anonymous User"  
published: 2015-11-12  
updated: 2015-11-12  
canonical: https://www.mindstick.com/forum/33588/how-to-use-comparevalidator-in-asp-dot-net  
category: ".net"  
tags: ["asp.net"]  
reading_time: 2 minutes  

---

# How to use CompareValidator in asp.net

I want to use CompareValidator for [Compare](https://www.mindstick.com/forum/2036/propertyinfo-getvalue-unable-to-compare-datetimes) [password](https://www.mindstick.com/blog/118/retriving-user-password-by-using-stored-procedure-in-asp-dot-net) in [asp.net](https://www.mindstick.com/articles/934/default-folders-available-inside-the-asp-dot-net-application-folder) how to use please help me

## Replies

### Reply by Anonymous User

The CompareValidator might not be the most commonly used validator of the bunch, but it's still useful in some cases. It can compare two values, for instance the values of two controls. In the next example, I will show you Compare password a small example of how it can be used.

```
 Password:<br /><asp:TextBox runat="server" id="txtpwd" TextMode="Password" /><br /><br />
Confirm Password:<br />
<asp:TextBox runat="server" id="txtconfirmpwd" TextMode="Password"  /><br />
<asp:CompareValidator runat="server" id="cmppwd" controltovalidate="txtpwd" controltocompare="txtconfirmpwd" operator="Equal" type="String" errormessage="Confirm Password does Not match!" /><br />
```

As you see, we only use one validator to validate the two fields. It might seem a bit overwhelming, but it's actually quite simple. Like with the RequiredFieldValidator, we use the controltovalidate attribute to specify which control to validate. In addition to that, we specify a control to compare. The operator attribute specifies which method to use when comparing. In this case, we use the LessThan operator, because we wish for the first control to have the password value. We set the type to string, because we want to compare password. Dates, strings and other value types can be compared as well. \
Now, try running the website, and test the two new fields. Here is what happens if you don't fill out the form correctly: \

```

```


---

Original Source: https://www.mindstick.com/forum/33588/how-to-use-comparevalidator-in-asp-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
