CompareValidator
One of the most useful validation controls is the CompareValidator control,
which performs a comparison between the data entered into a given control and
some other value. That other value can be a fixed value, such as a number, or a
value entered into another control.
Lets look at an example that builds on the login example from the previous sec-
tion. Here, well validate that the data entered into both the password fields is
identical. Make the following changes to Login.aspx:
File: Login.aspx (excerpt)
<asp:TextBox id="passwordTextBox" runat="server"
TextMode="Password" />
<asp:TextBox id="confirmPasswordTextBox" runat="server"
TextMode="Password" />
<asp:CompareValidator id="comparePasswords" runat="server"
ControlToCompare="passwordTextBox"
ControlToValidate="confirmPasswordTextBox"
ErrorMessage="Your passwords do not match up!"
Display="Dynamic" />
</p>
Run the page and enter different passwords into the two fields. The CompareVal-
idator control will appear as soon as you move on from two fields whose data
doesnt match, as Figure 6.5 shows.
Figure 6.5. A CompareValidator in action
231
CompareValidator
As youve probably noticed, the CompareValidator control differs very little from
the RequiredFieldValidator control:
File: Login.aspx (excerpt)
<asp:RequiredFieldValidator id="confirmPasswordReq" runat="server"
ControlToValidate="confirmPasswordTextBox"
ErrorMessage="Password confirmation is required!"
SetFocusOnError="True" Display="Dynamic" />
<asp:CompareValidator id="comparePasswords" runat="server"
ControlToCompare="passwordTextBox"
ControlToValidate="confirmPasswordTextBox"
ErrorMessage="Your passwords do not match up!"
Display="Dynamic" />
The only difference is that in addition to a ControlToValidate property, the
CompareValidator has a ControlToCompare property. We set these two properties
to the IDs of the controls we want to compare. So, in our example, the
ControlToValidate property is set to the confirmPasswordTextBox, and the
ControlToCompare property is set to the passwordTextBox.
The CompareValidator can be used to compare the value of a control to a fixed
value, too. CompareValidator can check whether the entered value is equal to,
less than, or greater than, any given value. As an example, lets add an age field
to our login form:
File: Login.aspx (excerpt)
<!-- Age -->
<p>
Age:<br />
<asp:TextBox id="ageTextBox" runat="server" />
<asp:RequiredFieldValidator id="ageReq" runat="server"
ControlToValidate="ageTextBox"
ErrorMessage="Age is required!"
SetFocusOnError="True" Display="Dynamic" />
<asp:CompareValidator id="ageCheck" runat="server"
Operator="GreaterThan" Type="Integer"
ControlToValidate="ageTextBox" ValueToCompare="15"
ErrorMessage="You must be 16 years or older to log in" />
</p>
In this case, the CompareValidator control is used to check that the user is old
enough to log in to our fictitious web application. Here, we set the Operator
property of the CompareValidator to GreaterThan. This property can take on
any of the values Equal, NotEqual, GreaterThan, GreaterThanEqual, LessThan,
232
Chapter 6: Using the Validation Controls

Get Build Your Own ASP.NET 2.0 Web Site Using C# & VB, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.