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.
<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: