Comparing Values: The CompareValidator Control


Comparing Values: The CompareValidator Control

The CompareValidator control performs comparisons between the data entered into a form field and another value. The other value can be a fixed value, such as a particular number, or a value entered into another control. All the properties and methods of the CompareValidator are listed in Table 3.3.

Table 3.3. CompareValidator Properties, Methods, and Events

Properties

Description

ControlToCompare

Specifies the ID of the control to use for comparing values.

ControlToValidate

Specifies the ID of the control that you want to validate.

Display

Sets how the error message contained in the Text property is displayed. Possible values are Static , Dynamic , and None ; the default value is Static .

EnableClientScript

Enables or disables client-side form validation. This property has the value True by default.

Enabled

Enables or disables both server and client-side validation. This property has the value True by default.

ErrorMessage

Specifies the error message that is displayed in the ValidationSummary control. This error message is displayed by the control when the Text property is not set.

IsValid

Has the value True when the validation check succeeds and False otherwise .

Operator

Gets or sets the comparison operator to use when performing comparisons. Possible values are Equal , NotEqual , GreaterThan , GreaterThanEqual , LessThan , LessThanEqual , DataTypeCheck .

Text

Sets the error message displayed by the control.

Type

Gets or sets the data type to use when comparing values. Possible values are Currency , Date , Double , Integer , and String .

ValueToCompare

Specifies the value used when performing the comparison.

Methods

Description

Validate

Performs validation and updates the IsValid property.

Events

Description

None

 

You can use CompareValidator , for example, to check whether a user entered a number greater than 7, check to see whether a date entered into one control is greater than a date entered into another control, or check whether a currency amount is less than a certain specified amount.

CompareValidator also can be used to check whether a form field contains a particular data type. For example, you can use the control to check whether a user entered a valid date, number, or currency value.

Comparing the Value of One Control to the Value of Another

Imagine that you have two form fields for entering dates: one labeled Start Date and one labeled End Date . Now, imagine that you want to make sure that any date entered into the second form field is later than any date entered in the first form field. You can perform this type of form validation by using the CompareValidator control.

To compare two dates, you need to set the ControlToValidate , ControlToCompare , Operator , and Type properties of the CompareValidator control. The page in Listing 3.12 illustrates how you can check whether one date is later than another.

Listing 3.12 CompareValidator.aspx
 <Script Runat="Server"> Sub Button_Click( s As Object, e As EventArgs )   If IsValid Then     Response.Redirect( "ThankYou.aspx" )   End If End Sub </Script> <html> <head><title>CompareValidator.aspx</title></head> <body> <form Runat="Server"> Start Date: <asp:TextBox   id="txtStartDate"   Columns="8"   Runat="Server"/> End Date: <asp:TextBox   id="txtEndDate"   Columns="8"   Runat="Server"/> <br> <asp:CompareValidator   ControlToValidate="txtEndDate"   ControlToCompare="txtStartDate"   Display="Dynamic"   Text="End date must be greater than start date!"   Operator="GreaterThan"   Type="Date"   Runat="Server" /> <p> <asp:Button   Text="Submit!"   OnClick="Button_Click"   Runat="Server"/> </form> </body> </html> 

The C# version of this code can be found on the CD-ROM.

The CompareValidator control in Listing 3.12 uses the ControlToValidate and ControlToCompare properties to indicate the controls to use for the comparison.

The Operator property has the value GreaterThan . CompareValidator checks whether the value entered into the txtEndDate control is greater than the value entered into the txtStartDate control.

Finally, the Type property has the value Date . The control compares the two values as date values rather than string or integer values.

CompareValidator does not check whether values are actually entered into the controls that it is comparing. If either the txtStartDate or txtEndDate controls are left blank, the form passes the validation check.

Comparing the Value of a Control to a Fixed Value

Instead of using CompareValidator to compare the values of two controls, you can use it to compare a value in one control to a fixed value. For example, suppose that you are building an auction Web site, and you want a user to enter a bid higher than a certain amount. You can use CompareValidator to compare a bid to the previous highest bid. The page in Listing 3.13 illustrates how you would do so.

Listing 3.13 CompareValidatorValue.aspx
 <Script Runat="Server"> Sub Button_Click( s As Object, e As EventArgs )   If IsValid Then     Response.Redirect( "ThankYou.aspx" )   End If End Sub </Script> <html> <head><title>CompareValidatorValue.aspx</title></head> <body> <form Runat="Server"> Minimum Bid: ,344.89 <br> Enter your bid: <asp:TextBox   id="txtBidAmount"   Columns="8"   Runat="Server"/> <asp:CompareValidator   ControlToValidate="txtBidAmount"   ValueToCompare="2,344.89"   Display="Dynamic"   Text="Your bid must be greater than the minimum bid!"   Operator="GreaterThan"   Type="Currency"   Runat="Server" /> <p> <asp:Button   Text="Submit"   OnClick="Button_Click"   Runat="Server"/> </form> </body> </html> 

The C# version of this code can be found on the CD-ROM.

If the user attempts to enter a bid less than $2,344.89, an error message is displayed. The fixed value is assigned to CompareValidator by using its ValueToCompare property.

Performing a Data Type Check

A common validation task involves performing data type checks. For example, you might need to check whether a user has entered a date in a date field, a string in a string field, or a number in a number field. You can perform this type of validation with the CompareValidator control by using the DataTypeCheck value of the Operator property.

Imagine that you have a form field for a user's birth date. The page in Listing 3.14 illustrates how you can check for a valid date.

Listing 3.14 CompareValidatorDataTypeCheck.aspx
 <Script Runat="Server"> Sub Button_Click( s As Object, e As EventArgs )   If IsValid Then     Response.Redirect( "ThankYou.aspx" )   End If End Sub </Script> <html> <head><title>CompareValidatorDataTypeCheck.aspx</title></head> <body> <form Runat="Server"> Enter your birth date: <asp:TextBox   id="txtBirthDate"   Columns="10"   Runat="Server"/> <asp:CompareValidator   ControlToValidate="txtBirthDate"   Display="Dynamic"   Text="Invalid birth date!"   Operator="DataTypeCheck"   Type="Date"   Runat="Server" /> <p> <asp:Button   Text="Submit"   OnClick="Button_Click"   Runat="Server"/> </form> </body> </html> 

The C# version of this code can be found on the CD-ROM.

In Listing 3.14, the CompareValidator control's Operator property is assigned the value DataTypeCheck , and the Type property is assigned the value Date . If an invalid date is entered into the txtBirthDate form field, the CompareValidator control displays an error.

CAUTION

The CompareValidator is pretty particular about the dates that it will accept. For example, the following dates are not considered valid:

 
 January 1, 2001 Jan 1, 2001 

The CompareValidator requires a date that looks like this:

 
 1/1/2001 1-1-2001 

If you want to be more inclusive when performing date validation, then you'll need to use the CustomValidator (described later in this chapter in the section entitled "Performing Custom Validation: The CustomValidator Control").




ASP.NET Unleashed
ASP.NET 4 Unleashed
ISBN: 0672331128
EAN: 2147483647
Year: 2003
Pages: 263

Similar book on Amazon

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