|< Day Day Up >|
16.4. Validation Controls
Table 16-5 lists the six built-in validation controls along with their unique properties and values.
Using Validation Controls
Validation controls are used only with controls that have a single input field type. These include the HTMLInputText, HTMLSelect, TextBox, DropDownList, and ListBox controls. ASP.NET implements the validation function on both the server and client side. To handle the client side, it includes a .js file containing validation code in the response to the browser. Note, however, that if scripting is disabled on the browser, only client-side checking will occur.
To illustrate the mechanics of using a validation control, here is the code to associate a RequiredFieldValidator and RangeValidator control with a TextBox control. The range validator control uses its MaximumValue and MinimumValue properties to ensure that the value entered in the text box is a numeric value from 0 to 12.
<td><asp:TextBox Width=30 id=hti maxlength=2 runat=server></td> <asp:RequiredFieldValidator runat=server ControlToValidate="hti" ErrorMessage="Must enter height value." Display="dynamic"> </asp:RequiredFieldValidator> <asp:RangeValidator ControlToValidate="hti" MaximumValue="12" MinimumValue="0" Type="Integer" Display="dynamic " ForeColor="Blue" ErrorMessage="Invalid Height." runat=server> </asp:RangeValidator>
This example also illustrates useful properties that all validator controls inherit from the BaseValidator class:
Of the controls listed in Table 16-4, the CustomValidator and ValidationSummary exhibit unique behavior that requires further explanation.
There are many common validation patterns that the built-in validation controls do not handle. For example, the contents of one control may be dependent on another, such as when a credit card number format depends on the type of card selected. Another common example is to require that a string entered in a field conform to a minimum and maximum length. Cases such as these are handled with a CustomValidator control that points to server-side and/or client-side routines that implement custom validation logic. To demonstrate how this control works, let's use it to validate a field that accepts a password, which must be between 8 and 12 characters in length.
The declaration of the control is similar to that of the other validation controls. The main difference is the ClientValidationFunction and OnServerValidate fields that specify client and server routines to validate the associated password field.
<input type=password runat="server" /> <br> <asp:CustomValidator ControlToValidate="pw" ClientValidationFunction="checkPWClient" OnServerValidate="checkPW" Display=dynamic ErrorMessage="A password must be between 8 and 12 characters." runat="server"/>
Two parameters are passed to the validation routines: source and args. Args is the more important of the two. Its value property exposes the content of the form field being validated. In this example, the length of the field value is checked. If it falls within the bounds, IsValid is set to true; otherwise, it is set to false. A false value triggers the error message defined by the CustomValidator control.
For consistency with the built-in validation controls, include both server- and client-side routines for custom validation. If only one is to be implemented, server side is always preferred.
This control collects all of the error messages generated by the validation controls and displays them as a list or customizable paragraph. The messages are displayed either within the control (as a <span> element within the HTML) or as a pop-up window by setting the ShowMessageBox to true.
<asp:ValidationSummary id=validsumm runat="server" ShowMessageBox=true DisplayMode=List ShowSummary=false> </asp:ValidationSummary>
The most important factor to consider when deciding how to display validation error messages is that the ValidationSummary control displays messages when a form is submitted, and individual validation controls display a message when their associated control loses focus. Thus, displaying the message at a validation control provides immediate feedback to the user. Also note that if the validation control has its Display property set to static or dynamic, the error message is displayed by both the validation control and the ValidationSummary control.
|< Day Day Up >|