Inserting a Validation Control

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

Table 8.1. BaseValidator Class Properties
Property Type Description
ControlToValidate String The name of the control this validator is supposed to validate.
Enabled Boolean Determines whether or not the validator control is active.
EnableClientScript Boolean Determines whether or not client-side validation is used.
ErrorMessage String Describes the error message to be used if validation fails. Used by the ValidationSummary control.
IsValid Boolean Is true if the ControlToValidate is valid and false otherwise. Set by the Validate() method.
Text String Describes the error message to be displayed on the page where the validator control is placed, if the validated control is not valid.
Display ValidatorDisplay [DynamicNoneStatic] Determines how the control is displayed on the page, if at all.

Validation Control Methods

All validation controls also expose the method described in Table 8.2.

Table 8.2. BaseValidator Class Methods
Method Type Description
Validate() void Causes the control to perform its validation and set the IsValid property.
I l @ ve RuBoard

Asp. Net. By Example
ASP.NET by Example
ISBN: 0789725622
EAN: 2147483647
Year: 2001
Pages: 154

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: