ASP.NET 2.0 provides a number of Web controls that can be used to declaratively validate user input on other ASP.NET controls. Simply put, associating a control with a validation control enables you to perform a specific sort of validation operation on the input that's made to the control. The Validation section of the Toolbox task pane lists the ASP.NET 2.0 validation controls. The validation controls are capable of performing both client-side validation (if the browser supports scripting) and server-side validation.
NOTE
For increased security and suppressing malicious validation, the validation controls are designed to always perform server-side validation even if the client-side validation has already been performed.
As discussed earlier, the process of validating user input to ASP.NET controls using validation controls is by associating the validation control with the ASP.NET control. This is usually done by setting the ControlToValidate property of the validation control. ASP.NET 2.0 makes the following validation controls available:
CompareValidator: This control is used to either compare the value of a control to a static value or compare the values of two controls on the Web page. You can also specify the operator for the comparison.
CustomValidator: Allows you to write a custom piece of code that can be used to validate user input to a control
RangeValidator: Can be used to find out if the user input to a control matches within a particular range of values
RegularExpressionValidator: Allows you to find out if the user input matches a pattern of characters defined by a regular expression syntax
RequiredFieldValidator: Allows you to constrain a user from typing empty values in the associated control
ValidationSummary: Sums up the validation errors of all the validation controls on the page in a single summary
The CauseValidation property of a control triggers the validation to be performed on the Web page. For example, submitting a form containing validation controls associated with the form controls by using an ASP.NET button control with the CauseValidation property set to true causes the validation to occur during the postback.
The following exercise takes you through the steps you need to take to implement validation on a number of form controls on form submit. You mandate user input on the controls by using the RequiredFieldValidator control, compare the inputs using a CompareValidator control, and display the error messages using a ValidationSummary control. Follow these steps:
Open an ASP.NET Web page in the Design view and then create a form with three ASP.NET TextBox controls and a Button control. Change the Text property of the Button control to Submit and then ensure that the CauseValidation property of this control is set to true. The sample form is shown in Figure 10.20.
Using the Validation section of the Toolbox task pane, insert a ValidationSummary control below the Button control inserted in step 1.
Insert three RequiredFieldValidator controls and a CompareValidator control at the bottom of the form.
Select the first RequiredFieldValidator control and then set its ControlToValidate property to the first TextBox control.
Set the ControlToValidate property of the other two RequiredFieldValidator controls to the corresponding TextBox controls.
Set the ErrorMessage property for all the RequiredFieldValidator controls appropriately. These should be the messages that display when user input isn't a match.
Select the CompareValidator control and then set its ControlToValidate and ControlToCompare properties to the appropriate TextBox controls (Start Date and End Date, respectively, for this exercise).
Set the Operator property of the CompareValidator control to Less Than and the Type property to Date. Also, set the ErrorMessage property of this control appropriately, as shown in Figure 10.21.
If you submit the form without filling in the text fields, the RequiredFieldValidator catches this and reports an error. Also, if the Start Date is greater than the End Date, the CompareValidator reports an error. The ValidationSummary control displays the summary of the errors.
3.145.74.63