There are six validation controls included with ASP.NET as well as a summary control. Data validation with the ASP.NET validation controls is always processed on the Web server and can also optionally be processed on the user's computer.
by Shannon Horn
Aug 7, 2003
Page 3 of 8
Use the ASP.NET CompareValidator control to compare the value in a control to either a static value or to the value in another control. You link CompareValidator to a control in the same manner as the RequiredFieldValidator control (via the ControlToValidate attribute). The CompareValidator control performs pass or fail validation in the same manner as the RequiredFieldValidator control.
The HTML snippet below illustrates a TextBox control and a CompareValidator control where the validation control compares the value entered into the textbox against a static value that is stated in the validation control markup. As with the RequiredFieldValidator control, when you choose to enable validation on the user's computer, the error message will display when your user navigates away from the textbox control if validation fails.
ErrorMessage="User name not found."
The snippet above creates a TextBox control linked to a CompareValidator control. You already know what some of the attributes are. The attributes shown above that I need to explain are: ValueToCompare, Operator, and Type.
Just as its name states, the ValueToCompare attribute specifies the static value to compare the value in the ControlToValidate control to. In the example snippet above, the value is a name ("Shannon").
The RequiredFieldValidator control is the only data validation control that will fail the data validation process if the control that it is linked to contains no value.
The Operator attribute specifies the operator to use for comparison. The possible values for the Operator attribute are: DataTypeCheck, Equal, GreaterThan, GreaterThanEqual, LessThan, LessThanEqual, and NotEqual. The name of each should be self-explanatory as to what comparison will be made. The DataTypeCheck value will determine if the values are of the same data type.
The Type attribute specifies the data type of the comparison to be made. The possible values for the Type attribute are: Currency, Date, Double, Integer, and String. For an excerpt from the generated page, see Figure 3.