LessThanEqual, or DataTypeCheck, which well look at shortly. Next, we tell the
CompareValidator control to compare the two values by setting the Type property
to Integer, which will cause the CompareValidator to treat the values as whole
numbers (this property can also be set to Currency or Date, among other options).
Finally, we use the ValueToCompare property to make sure that the users age is
greater than 15. If you load this page in your web browser now, youll see that
the form is only validated when the user enters an age of 16 or more.
We can also use the CompareValidator control to perform data type checks. To
see how this works, lets replace the age TextBox control with a date-of-birth text
box, whose value must be a valid date:
File: Login.aspx (excerpt)
<!-- Birth Date -->
<p>
Birth Date:<br />
<asp:TextBox id="birthDateTextBox" runat="server" />
<asp:CompareValidator id="birthDateCheck" runat="server"
Operator="DataTypeCheck" Type="Date"
ControlToValidate="birthDateTextBox"
ErrorMessage="You must enter the date in a valid format!"
SetFocusOnError="True" Display="Dynamic" />
</p>
As you can see, the Operator property of the CompareValidator control is set
to perform a DataTypeCheck, and the Type property is set to Date. Load the page,
and youll see that you cant enter anything other than a valid date into this field.
The constituents of a valid date will depend on the regional settings on your
web server.
RangeValidator
The RangeValidator control checks whether the value of a form field falls between
minimum and maximum values. For instance, we could make sure that users who
visit our web site were born in a certain decade. If they enter values that dont
fit into the range we specify, the validator will return an invalid message.
Lets continue by expanding Login.aspx even further:
File: Login.aspx (excerpt)
<!-- Birth Date -->
<p>
Birth Date:<br />
233
RangeValidator
<asp:TextBox id="birthDateTextBox" runat="server" />
<asp:RangeValidator id="birthDateRangeTest" runat="server"
Type="Date" ControlToValidate="birthDateTextBox"
MinimumValue="1/1/1970" MaximumValue="12/31/1979"
ErrorMessage="You must've been born in the 1970s to use
this web site!" />
</p>
Take Care when Specifying Dates
If youre outside of the US, you may need to modify the above example. In
the US, dates are specified in month-day-year format. In the UK and Aus-
tralia, theyre specified in day-month-year order, and in other countries, the
year is specified first. The ASP.NET runtime will be expecting you to specify
dates in your local format, so adjust the values of the MinimumValue and
MaximumValue properties accordingly.
Here, weve added a RangeValidator to validate the birthDateTextBox control.
Our RangeValidator control checks whether the date entered falls within the
1970s, and shows an error message similar to Figure 6.6 if it doesnt.
Figure 6.6. Using the RangeValidator control
Note that the Type property of the RangeValidator control specifies the data
type thats expected in the control with which its associated: if some other data
type is entered into this field, it fails validation. As such, we've removed the
CompareValidator we added for this purpose.
234
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.