You are here:   Units 5 to 8 > 6. Validation
  |  Login

Validation

Minimize

Validating the User Input


People don't agree on many things but on this one there is general consensus: "Never trust user input".  Indeed, the user can do anything: can input bogus data just to have fun, can try to break your site, can try to hack it, or he can simply make an honest mistake. If you do not control the user input with validation actions, you will never know how your website will react.  This can lead to unpleasant consequences.

As such, ASP.NET employs validation controls to help you validate the user input before it is used in your website.

The validation controls can be found in the Toolbox under the Validation section, as in the following figure.

 

 

 

 

Form Validation Controls


Included in the Web Forms is a set of validation server controls that provide an easy-to-use but powerful way to check input forms for errors and if necessary, display messages to the user. Validation controls are added to a Web Forms page like other server controls.and there are controls for specific types of validation, such as range checking or pattern matching, plus a RequiredFieldValidator that ensures that a user does not skip an entry field. You can attach more than one validation control to an input control. – For example, you might specify both that an entry is required and that it must contain a specific range of values.

Validation controls work with a limited subset of HTML and Web server controls. For each control, a specific property contains the value to be validated.

The following table lists the input controls that may be validated.

Control Validation Property
HtmlInputText Value
HtmlTextArea Value
HtmlSelect Value
HtmlInputFile Value
TextBox Text
ListBox SelectedItem.Value
DropDownList SelectedItem.Value
RadioButtonList SelectedItem.Value

 

The validation controls always perform validation checking in server code. However, if the user is working with a browser that supports DHTML, the validation controls can also perform validation using client script. With client-side validation, any errors are detected on the client when the form is submitted to the server. If any of the validators are found to be in error, the submission of the form to the server is cancelled and the validator's Text property is displayed. This permits the user to correct the input before submitting the form to the server. Field values are revalidated as soon as the field containing the error loses focus, thus providing the user with a rich, interactive validation experience.

Note that the Web Forms page framework always performs validation on the server – even if the validation has already been performed on the client. This helps prevent users from being able to bypass validation by impersonating another user or a preapproved transaction. Client-side validation is enabled by default.. If the client is capable, uplevel validation will be performed automatically. To disable client-side validation – Set the page's ClientTarget property to "Downlevel" ("Uplevel" forces client-side validation).

 

 

 

Displaying Validation Errors


When the user's input is processed (for example, when the form is submitted) – The Web Forms page framework passes the user's entry to the associated validation control or controls. The validation controls test the user's input and sets a property to indicate whether the entry passed the validation test. After all validation controls have been processed, the IsValid property on the page is set. If any of the controls shows that a validation check failed, the entire page is set to invalid.

Required Field Validator

The requried field validator checks a control to make sure that data has been supplied. This is usually used on text boxes to make sure that a form has been completely filled out.

The following video shows how to use the Required Field Validator control.

Range Validator

The range validator is used to check to make sure that value of a control falls within a specific range.  Watch the next video to see how to use it.

Compare Validator

The Compare Validator Control is used to compare the contents of a control with a value or another control. The following video shows you how to use this control.

Validation Summary

The validation summary control is as way of centralizing error messages that could be generated from multiple validation controls.  This video will show how easy is to use this control.