Creating Custom Validation Controls Summary In this chapter, you learn how to validate form fields when a form is submitted to the web server. You can use the validation controls to prevent users from submitting the wrong type of data into a database table. For example, you can use validation controls to prevent a user from submitting the value "Apple" for a birth date field.
In the first part of this chapter, you are provided with an overview of the standard validation controls included in the ASP. You learn how to control how validation errors are displayed, how to highlight validation error messages, and how to use validation groups. You are provided with sample code for using each of the standard validation controls. Next, we extend the basic validation controls with our own custom validation controls.
For example, you learn how to create an AjaxValidator control that enables you to call a server-side validation function from the client. RequiredFieldValidator— Enables you to require a user to enter a value in a form field.
RangeValidator— Enables you to check whether a value falls between a certain minimum and maximum value. CompareValidator— Enables you to compare a value against another value or perform a data type check. RegularExpressionValidator— Enables you to compare a value against a regular expression. CustomValidator— Enables you to perform custom validation. ValidationSummary— Enables you to display a summary of all validation errors in a page. You can associate the validation controls with any of the form controls included in the ASP.
For example, if you want to require a user to enter a value into a TextBox control, then you can associate a RequiredFieldValidator control with the TextBox control. Note Technically, you can use the validation controls with any control that is decorated with the ValidationProperty attribute.
The page in Listing 3. It contains three TextBox controls that enable you to enter a product name, product price, and product quantity.
Each of the form fields are validated with the validation controls. If you attempt to submit the form in Listing 3. This property accepts the name of the control to validate on the page. The first CompareValidator is used to check whether the txtProductPrice text field contains a currency value, and the second CompareValidator is used to check whether the txtProductQuantity text field contains an integer value. Notice that there is nothing wrong with associating more than one validation control with a form field.
If you need to make a form field required and check the data type entered into the form field, then you need to associate both a RequiredFieldValidator and CompareValidator control with the form field. Finally, notice that the Page. When using the validation controls, you should always check the Page. IsValid property before doing anything with the data submitted to a page.
This is done for security reasons. If someone creates a fake form and submits the form data to your web server, the person still won't be able to submit invalid data. If you prefer, you can disable client-side validation for any of the validation controls by assigning the value False to the validation control's EnableClientScript property. IsValid As mentioned earlier, you should always check the Page. IsValid property when working with data submitted with a form that contains validation controls.
Each of the validation controls includes an IsValid property that returns the value True when there is not a validation error. IsValid property returns the value True when the IsValid property for all of the validation controls in a page returns the value True. It is easy to forget to check the Page.
For example, if you request the page in Listing 3. IsValid property is used in Listing 3. Warning Unfortunately, I've made the mistake of forgetting to include a check of the Page. IsValid property several times when building applications.
Because you do not normally develop a web application with a downlevel browser, you won't notice the problem described in this section until you start getting invalid data in your database tables. Setting the Display Property All the validation controls include a Display property that determines how the validation error message is rendered.
This property accepts any of the following three possible values: When the Display property has this value, the validation error message rendered by the validation control looks like this: If, on the other hand, you set the Display property to the value Dynamic, the error message is rendered like this: Both the visibility and display attributes can be used to hide text in a browser. However, text hidden with the visibility attribute still occupies screen real estate.
Text hidden with the display attribute, on the other hand, does not occupy screen real estate. In general, you should set a validation control's Display property to the value Dynamic. That way, if other content is displayed next to the validation control, the content is not pushed to the right.
The third possible value of the Display property is None. If you prefer, you can prevent the individual validation controls from displaying an error message and display the error messages with a ValidationSummary control. You learn how to use the ValidationSummary control later in this chapter.
Highlighting Validation Errors When a validation control displays a validation error, the control displays the value of its Text property. Normally, you assign a simple text string, such as " Required " to the Text property. For example, the page in Listing 3. Another way that you can emphasize errors is to take advantage of the SetFocusOnError property that is supported by all the validation controls. When this property has the value True, the form focus is automatically shifted to the control associated with the validation control when there is a validation error.
If you provide a value for the first text field and not the second text field and submit the form, the form focus automatically shifts to the second form field. This property exposes the collection of all the validation controls in a page. Validators property is used to highlight each control that has a validation error see Figure 3.
The IsValid property is checked for each control in the Page. If IsValid returns False, then the control being validated by the validation control is highlighted with a yellow background color. NET Framework, there was no easy way to add two forms to the same page.
If you added more than one form to a page, and both forms contained validation controls, then the validation controls in both forms were evaluated regardless of which form you submitted. For example, imagine that you wanted to create a page that contained both a login and registration form. The login form appeared in the left column and the registration form appeared in the right column.
If both forms included validation controls, then submitting the login form caused any validation controls contained in the registration form to be evaluated.
A validation group enables you to group related form fields together. The controls associated with the login form all have the value "LoginGroup" assigned to their ValidationGroup properties. The controls associated with the register form all have the value "RegisterGroup" assigned to their ValidationGroup properties. Because the form fields are grouped into different validation groups, you can submit the two forms independently. Submitting the Login form does not trigger the validation controls in the Register form see Figure 3.
You can assign any string to the ValidationGroup property. The only purpose of the string is to associate different controls in a form together into different groups. Note Using validation groups is particularly important when working with Web Parts because multiple Web Parts with different forms might be added to the same page. If you assign the value False to this property, then clicking the button bypasses any validation in the page.
Bypassing validation is useful when creating a Cancel button. If the button did not include this property, then the RequiredFieldValidator control would prevent you from submitting the form when you clicked the Cancel button.