HOW TO USE COMPAREVALIDATOR IN ASP.NET

Ankit Singh

Total Post:341

Points:2389
Posted by  Ankit Singh
 593  View(s)
Ratings:
Rate this:
I want to use CompareValidator for Compare password in asp.net how to use please help me
  1. aditya kumar Patel

    Post:254

    Points:1810
    Re: How to use CompareValidator in asp.net

    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:

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!