The CompareValidator Control

CompareValidator Control"-->

only for RuBoard

The CompareValidator Control

The next validator control we'll be talking about is the CompareValidator control. The CompareValidator differs from the previous validator controls because it enables you to compare the value entered by the user with another value. The other value can be that of either another control or a constant value. For instance, if you have a site that has membership that is password controlled during user registration, it is customary to have the user enter their password twice to lessen the chance of entering it incorrectly. You could use the CompareValidator and quickly wire the two Password TextBox controls together to ensure the same value was entered into both.

You can use a couple different attributes to wire up controls to the CompareValidator . The following list contains a description of each:

  • ControlToCompare The ID attribute of the INPUT control you want to compare against. Do not confuse this with the ControlToValidate . ControlToValidate will be the control you want to compare to the ControlToCompare control.

  • ValueToCompare A constant value you want to compare against.

The CompareValidator doesn't just support comparing string against string. By using the Type attribute, you can specify what data type you want validated . The following list contains each supported data type:

  • Currency

  • Date

  • Double

  • Integer

  • String

Not only can you ensure that two values are equal, you can choose from the following seven compare operators, which would be applied to the Operator attribute of the control:

  • Equal ” The values should equal to one another.

  • GreaterThan ” The ControlToValidate value must be greater than the ControlToCompare or ValueToCompare value.

  • GreaterThanEqual ” The ControlToValidate value must be equal to or greater than the ControlToCompare value or ValueToCompare .

  • LessThan ” The ControlToValidate value must be less than the ControlToCompare or ValueToCompare value.

  • LessThanEqual ” The ControlToValidate value must be less than or equal to the ControlToCompare or ValueToCompare value.

  • NotEqual ” The ControlToValidate value cannot equal the ControlToCompare or ValueToCompare value.

  • DataTypeCheck ” The data type of the ControlToValidate must be the same as the ControlToCompare or ValueToCompare data type.

Listing 9.6 demonstrates how to implement the CompareValidator . This example compares the values of two different dates.

Listing 9.6 Comparing two dates
 01: <html> 02:  <body style="font-size:10"> 03:   <form runat="server"> 04: 05:    Beginning Date: 06:    <asp:textbox 07:     runat="server" 08:     id="BeginningDate" 09:    /> 10: 11: 12:    Ending Date: 13:    <asp:textbox 14:     runat="server" 15:    id="EndDate" 16:     Text="01/11/2001" 17:    /> 18: 19:    <p> 20: 21:    <asp:CompareValidator 22:     runat="server" 23:     ControlToValidate="BeginningDate" 24:     ControlToCompare="EndDate" 25:     Operator="LessThan" 26:     Type="Date" 27:     ErrorMessage="You MUST Enter A Valid Date And The Beginning Date Must Not Be graphics/ccc.gif Greater Than Ending Date" 28:    /> 29: 30:    <p> 31: 32:    <asp:Button 33:     runat="server" 34:     Text="Submit" 35:    /> 36: 37:   </form> 38:  </body> 39: </html> 

The CompareValidator from Listing 9.6 is located on lines 21 “28, and has the ControlToValidate set to the TextBox located on lines 6 “9, and the ControlToCompare set to the TextBox on lines 12 “17. The Operator is set to LessThan with a Type equal to Date . When the page initially loads, a date is already populated in the ControlToCompare TextBox and the ControlToValidate is blank. Now that you have the page up, do the following:

  1. Enter a date greater than the ControlToValidate and try to submit the page. An error message will be displayed.

  2. Enter a date less than the ControlToValidate and try to submit the page. The page will submit.

  3. Now enter nothing into the ControlToValidate and try to submit the page. The page will submit.

  4. Now enter a date larger than the ControlToValidate and then delete the ControlToValidate 's date or enter in something other than a date and try to submit the page. The page will submit.

Well, now you might be confused as to why the page is submitting for 3 and 4. If you think about it, it's logical. When we do number 3, the InitialValue of the property is never changed so validation doesn't occur. We can solve this problem by wiring up a RequiredField Validator on it. As for number 4, by deleting the value for the ControlToValidate to compare against, it will always return true . You also can solve this situation by wiring up a RequiredFieldValidator to it. You will find more often than not you will have to wire up more than one validator control to any one control to achieve proper validation. Later in this chapter, we will discuss how to wire up multiple validator controls to one INPUT control.

only for RuBoard


Programming Data-Driven Web Applications with ASP. NET
Programming Data-Driven Web Applications with ASP.NET
ISBN: 0672321068
EAN: 2147483647
Year: 2000
Pages: 170

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