|I l @ ve RuBoard|
Inserting a validation control is no different from inserting any other control. If you are using Visual Studio .NET, you can simply drag the appropriate control onto your page's layout. Otherwise , simply insert the appropriate control within the HTML where you would like the validation's message to appear if the input is not valid. The following example demonstrates how to require a user to enter his or her name .
Perhaps the most common form of validation is simply to require that fields be filled in. The RequiredFieldValidator control does just this. Listing 8.1 shows the relevant code to make a text box entry required (that is, to ensure that the user enters some value).
Listing 8.1 Adding required validation to a text box.
<form method="post" runat="server"> <table> <tr> <td> Full Name: </td> <td> <asp:textbox runat="Server" columns="50" id="fullname"> </asp:textbox> </td> <td> <asp:requiredfieldvalidator runat="Server" id="fullname_required" controltovalidate="fullname" errormessage="A name is required." display="dynamic"> * </asp:requiredfieldvalidator> </td> </tr> </table> </form>
Figure 8.1 shows an example of what this page would look like if the user attempted to submit the form without providing a value for the fullname text box.
Figure 8.1. A Simple Required Validator.
In Figure 8.1, note the * to the right of the field. This is the default indicator that a field has failed a validation test. We'll see how to display more helpful validation feedback shortly.
The examples for this chapter are available live online at:
One important thing to note is that all validator controls must be enclosed within server-side form tags. If you're not already doing so, I recommend that you follow Visual Studio's guidelines and simply place a server-side form on every .aspx page enclosing the entire contents of the <body> . This will save you a great deal of frustration trying to figure out why your controls are not behaving as you expect because you forgot the <form> tag.
In Listing 8.1, you see that the RequiredFieldValidator has a number of properties, as well as the contents of the tag, which in this case is simply a " * ". The contents of the tag are the control's Text property. When a validator control is displayed, the Text property determines what is displayed by default. However, the ValidationSummary control uses the ErrorMessage property, not the Text property. We will see later in this chapter how we can use both the Text and ErrorMessage properties together to offer the user a detailed description of the errors encountered in their form submission. First, let's go over the properties and methods that are common to all validation controls.
Validation Control Properties
All validation controls inherit from the System.Web.UI.WebControls. BaseValidator class, and so they share the properties detailed in Table 8.1.
You can view the complete class definition for this and any other .NET class by using the class browser application included with the SDK or available online at http://www.gotdotnet.com/quickstart/aspplus/samples/classbrowser/vb/classbrowser.aspx.
Table 8.1. BaseValidator Class Properties
Validation Control Methods
All validation controls also expose the method described in Table 8.2.
Table 8.2. BaseValidator Class Methods
|I l @ ve RuBoard|