NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Validating Against a Specific Value

You can validate a user's entry against a specific single value using logical operators. For example, you can specify that the user's entry is a date after January 1, 1950 or that it is an integer value greater than or equal to zero. Alternatively, you can specify that the user's entry be compared against a value from another control.

To validate against a specific value

  1. Add a CompareValidator control to the page and set the following properties:
    Property Setting
    ControlToValidate The ID of the control to validate.
    ErrorMessage, Text, ValidationDisplay Properties that specify the text and location of the error or errors that will display if the user skips the control. For details, see Controlling Validation Error Message Display.
  2. Set the value to compare to by setting the following properties:
    • ValueToCompare or ControlToCompare   An expression entered as a string. Set ValueToCompare to compare to a constant value. Set ControlToCompare to the ID of another control to compare against. (The CompareValidator control compares the user's entry against whatever property is specified by the other control's ValidationProperty property.) If you set both ValueToCompare and ControlToCompare, ControlToCompare takes precedence.
      Note   When validation is done against another control, invalid values in the other control are ignored and the validation passes. For details, see Special-Case Validation Results.
      Note   The validation control validates user input using the format specified by the page's locale setting .
    • Type The data type of the two values to be compared. Types are specified using the ValidationDataType enumeration, which allows you to use the type name String, Integer, Double, DateTime, or Currency. The values are converted to this type before the comparison is performed. If the values can't be converted — for example, the user's entry cannot be converted to a date — the validation fails.
    • Operator   The comparison to use. Operators are specified using the ValidationCompareOperator enumeration, which allows you to enter the name of the comparison operators, such as Equal, NotEqual, GreaterThan, GreaterThanEqual, and so on.
Note   If the user leaves a control blank, the control passes the comparison validation. To force the user to enter a value, add a RequiredField validation control as well. For details, see Validating a Required Entry.
  1. Add a test in your Web Forms code to check for validity. For details, see Testing Validity Programmatically.

The following shows an example in the .aspx file of a Web Form Textbox control with required field validation. A table is used to control layout.

<TABLE>
<TR>
<TD>
   <asp:Textbox id="txtAge" runat="server"></asp:Textbox>
</TD>
<TD>
   <asp:CompareValidator id="CompareFieldValidator1" runat="server"
      ForeColor="Red"
      ControlToValidate="txtAge"
      ValueToCompare=0
      Type="Integer"
      Operator="GreaterThanEqual"
      ErrorMessage="Please enter a whole number zero or greater.">
   </asp: CompareValidator >
</TD>
</TR>
</TABLE>

The following example shows a CompareValidator that compares the user's entry against the value in another control. In a form that allows users to make reservations at a hotel, the validator checks that the user does not enter a departure date earlier than the arrival date.

<TABLE>
<TR>
<TD>
   <asp:Textbox id="txtDepartureDate" runat="server"></asp:Textbox>
</TD>
<TD>
   <asp:CompareValidator id="CompareFieldValidator1" runat="server"
      ForeColor="Red"
      ControlToValidate="txtDepartureDate"
      ControlToCompare="txtArrivalDate"
      Type="DateTime"
      Operator="GreaterThanEqual"
      ErrorMessage="Departure date cannot be earlier than arrival date.">
   </asp: CompareValidator >
</TD>
</TR>
</TABLE>

See Also

Validation Controls | Introduction to Validating User Input in Web Forms | Controlling Validation Error Message Display | Controlling Client versus Server Validation