19.4. Checking and Comparing Values
The CompareValidator control offers three validators in one: compare values in two ASP.NET controls, compare a value in a control against a constant value, and test whether a user has entered a valid data type.
The available operators are Equal, NotEqual, GreaterThan, GreaterThanEqual, LessThan, LessThanEqual, and DataTypeCheck.
The operator names are self-explanatory. For example, GreaterThanEqual validates when the input value is greater than or equal to a second control's value (or a constant value).
19.4.1. Comparing values in two controls
The CompareValidator can determine whether the value in one text box is greater than the value in a second text box. Follow these steps to display an error message if the validation test fails:
Add two ASP.NET TextBox controls to a Web form.
Add a Button control to the page.
From the Validation category of the Toolbox, add a CompareValidator to the page.
In the CompareValidator control's Properties window, set the following properties and corresponding values:
Property Value ControlToCompare TextBox2 (or whatever your second text box is named) ControlToValidate TextBox1 (or whatever yours is called) Display Dynamic ErrorMessage The top ^ must be greater! Operator GreaterThan SetFocusOnError True Type Double
As shown in Figure 19-1, the CompareValidator complains at runtime if the value in the first text box isn't greater than the value in the bottom text box.
Figure 19-1. The Compare Validator judging text boxes.
Get ASP.NET 3.5 For Dummies® now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.