In this tutorial, we will cover the basics of ASP.NET validation techniques. This involves using more advanced validation controls and ValidationGroups to group validation controls together.

Form validation is important for the recipient to collect the correct information and for the user to send the correct information. It isn’t uncommon for a webpage to have multiple forms. Your front page may have a voting form and email newsletter signup form. Without validation groups the validations would interfere with each other by not allowing the user to submit either form unless both forms were filled out correctly. Validation groups allow the developer to set groups for each validation control and button.

First you can start a new Web Application in Visual Studio 2008. We are going to create a form. This form is going to contain a DropDownList, RangeValidator, and Button control. You can add all of these controls through the Toolbox. Below is an example of them.

Notice that the RangeValidator and Button control both have the ValidationGroup attribute set to the same name. This allows the application only to check the DropDownList’s validity when the user clicks the Submit button.

For tutorial purposes, we will place another form on the same page. This form will contain a Calendar control, 3 TextBox controls, and 3 validator controls. The first 2 TextBox controls will be for entering in the user’s email address twice. The first TextBox control will belong to a RequiredFieldValidator and CompareValidator. The RequireFieldValidator will make sure the user has entered an email address. The CompareValidator will make sure the user has entered the same email address twice. The Calendar and TextBox will allow the user to select a date for the form. A RangeValidator control will be used to make sure the date is in the month of December 2008.

Notice the Type attribute for the CompareValidator and RangeValidator control. If the value being validated is not an integer, the validation controls will not work properly unless their correct Type is set. All of the validation controls also have the ValidationGroup attribute set as valGroup2. This will distinguish the validation for this form from the validation in the first form.

To make this example complete we will need to program our application to set the TextBox date to the date selected by the calendar. You can do this by double-clicking the Calendar control in design mode. Now set the TextBox to the selectedDate formated to match our RangeValidator.

Due to the fact that the validation controls use Javascript, the page will instantly display an error when a form control isn’t valid. The user will not be allowed to submit the form until all fields for the current form are valid. We also placed the Calendar control in AJAX controls so choosing the date will not refresh the entire page. It is important to note that the error messages can be organized together in a list by using the ValidationSummary control.

You have successfully learned how to use a validation control on a date field and matching email address fields. You have also learned how to add two different forms to a page with separate validation groups.
Download Source Files