Using the CompareValidator Control


Using the CompareValidator Control

The CompareValidator control enables you to perform three different types of validation tasks. You can use the CompareValidator to perform a data type check. In other words, you can use the control to determine whether a user has entered the proper type of value into a form field, such as a date in a birth date field.

You also can use the CompareValidator to compare the value entered into a form field against a fixed value. For example, if you are building an auction website, you can use the CompareValidator to check whether a new minimum bid is greater than the previous minimum bid.

Finally, you can use the CompareValidator to compare the value of one form field against another. For example, you use the CompareValidator to check whether the value entered into the meeting start date is less than the value entered into the meeting end date.

The CompareValidator has six important properties:

  • ControlToValidate The ID of the form field being validated.

  • Text The error message displayed when validation fails.

  • Type The type of value being compared. Possible values are String, Integer, Double, Date, and Currency.

  • Operator The type of comparison to perform. Possible values are DataTypeCheck, Equal, GreaterThan, GreaterThanEqual, LessThan, LessThanEqual, and NotEqual.

  • ValueToCompare The fixed value against which to compare.

  • ControlToCompare The ID of a control against which to compare.

The page in Listing 3.10 illustrates how you can use the CompareValidator to perform a data type check. The page contains a birth date field. If you enter a value that is not a date, then the validation error message is displayed (see Figure 3.8).

Figure 3.8. Performing a data type check.


Listing 3.10. ShowDataTypeCheck.aspx

<%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head  runat="server">     <title>Show Data Type Check</title> </head> <body>     <form  runat="server">     <div>     <asp:Label                  Text="Birth Date:"         AssociatedControl         Runat="server" />     <asp:TextBox                  Runat="server" />     <asp:CompareValidator                  Text="(Invalid Date)"         ControlToValidate="txtBirthDate"         Type="Date"         Operator="DataTypeCheck"         Runat="server" />     <br /><br />     <asp:Button                  Text="Submit"         Runat="server" />     </div>     </form> </body> </html> 

Notice that the page in Listing 3.10 contains a CompareValidator control. Its Type property has the value Date, and its Operator property has the value DataTypeCheck. If you enter a value other than a date into the birth date field, the validation error message is displayed.

Warning

An important limitation of the CompareValidator concerns how it performs a data type check. You cannot enter a long date into the form in Listing 3.10 (for example, December 25, 1966). You must enter a short date (for example, 12/25/1966). When validating currency amounts, you cannot enter the currency symbol. If these limitations concern you, you can use either the RegularExpression or CustomValidator controls to perform a more flexible data type check.


You can also use the CompareValidator to perform a comparison against a fixed value. For example, the page in Listing 3.11 uses a CompareValidator to check whether a date entered into a form field is greater than the current date (see Figure 3.9).

Listing 3.11. ShowFixedValue.aspx

<%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server">     Sub Page_Load()         cmpDate.ValueToCompare = DateTime.Now.ToString("d")     End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head  runat="server">     <title>Show Fixed Value</title> </head> <body>     <form  runat="server">     <div>     <asp:Label                  Text="Date:"         AssociatedControl         Runat="server" />     <asp:TextBox                  Runat="server" />     <asp:CompareValidator                  Text="(Date must be greater than now)"         ControlToValidate="txtDate"         Type="Date"         Operator="GreaterThan"         Runat="server" />     <br /><br />     <asp:Button                  Text="Submit"         Runat="server" />     </div>     </form> </body> </html> 

Figure 3.9. Comparing a form field against a fixed value.


Finally, you can use a CompareValidator to compare the value of one form field against another form field. The page in Listing 3.12 contains a meeting start date and meeting end date field. If you enter a value into the first field that is greater than the second field, a validation error is displayed (see Figure 3.10).

Figure 3.10. Comparing two form fields.


Listing 3.12. ShowCompareValues.aspx

<%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head  runat="server">     <title>Show Compare Values</title> </head> <body>     <form  runat="server">     <div>     <asp:Label                  Text="Start Date:"         Runat="server" />     <asp:TextBox                  Runat="server" />     <br /><br />     <asp:Label                  Text="End Date:"         Runat="server" />     <asp:TextBox                  Runat="server" />     <asp:CompareValidator                  Text="(End date must be greater than start date)"         ControlToValidate="txtEndDate"         ControlToCompare="txtStartDate"         Type="Date"         Operator="GreaterThan"         Runat="server" />     <br /><br />     <asp:Button                  Text="Submit"         Runat="server" />     </div>     </form> </body> </html> 

Just like the RangeValidator, the CompareValidator does not display an error if you don't enter a value into the form field being validated. If you want to require that a user enter a value, then you must associate a RequiredFieldValidator control with the field.




ASP. NET 2.0 Unleashed
ASP.NET 2.0 Unleashed
ISBN: 0672328232
EAN: 2147483647
Year: 2006
Pages: 276

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net