LessThanEqual, or DataTypeCheck, which we’ll 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 user’s age is
greater than 15. If you load this page in your web browser now, you’ll 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, let’s 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 you’ll see that you can’t 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 don’t
fit into the range we specify, the validator will return an “invalid” message.
Let’s continue by expanding Login.aspx even further:
File: Login.aspx (excerpt)
<!-- Birth Date -->
<p>
Birth Date:<br />
233
RangeValidator