How to use CompareValidator in asp.net
1387
12-Nov-2015
I want to use CompareValidator for Compare password in asp.net how to use please help me
Anonymous User
12-Nov-2015The 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.
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: