Home > DeveloperSection > Forums > How to use CompareValidator in asp.net
Ankit Singh

Total Post:341

Points:2389
Posted on    November-12-2015 10:10 PM

 .NET ASP.Net 
Ratings:


 1 Reply(s)
 297  View(s)
Rate this:
I want to use CompareValidator for Compare password in asp.net how to use please help me


aditya kumar Patel

Total Post:250

Points:1782
Supported
Posted on    November-12-2015 10:12 PM

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:


Don't want to miss updates? Please click the below button!

Follow MindStick