- Overview of the Validation Controls
- Using the RequiredFieldValidator Control
- Using the RangeValidator Control
- Using the CompareValidator Control
- Using the RegularExpressionValidator Control
- Using the CustomValidator Control
- Using the ValidationSummary Control
- Creating Custom Validation Controls
- Summary
Using the RequiredFieldValidator Control
The RequiredFieldValidator control enables you to require a user to enter a value into a form field before submitting the form. You must set two important properties when using the RequiredFieldValdiator control:
- ControlToValidate— The ID of the form field being validated.
- Text— The error message displayed when validation fails.
The page in Listing 3.1 illustrates how you can use the RequiredFieldValidator control to require a user to enter both a first and last name (see Figure 3.5).
Figure 3.5 Requiring a user to enter form field values.
Example 3.7. ShowRequiredFieldValidator.aspx
<%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server"> <title>Show RequiredFieldValidator</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label id="lblFirstName" Text="First Name:" AssociatedControlID="txtFirstName" Runat="server" /> <br /> <asp:TextBox id="txtFirstName" Runat="server" /> <asp:RequiredFieldValidator id="reqFirstName" ControlToValidate="txtFirstName" Text="(Required)" Runat="server" /> <br /><br /> <asp:Label id="lblLastName" Text="Last Name:" AssociatedControlID="txtLastName" Runat="server" /> <br /> <asp:TextBox id="txtLastName" Runat="server" /> <asp:RequiredFieldValidator id="reqLastName" ControlToValidate="txtLastName" Text="(Required)" Runat="server" /> <br /><br /> <asp:Button id="btnSubmit" Text="Submit" Runat="server" /> </div> </form> </body> </html>
By default, the RequiredFieldValidator checks for a nonempty string (spaces don't count). If you enter anything into the form field associated with the RequiredFieldValidator, then the RequiredFieldValidator does not display its validation error message.
You can use the RequiredFieldValidator control's InitialValue property to specify a default value other than an empty string. For example, the page in Listing 3.8 uses a RequiredFieldValidator to validate a DropDownList control (see Figure 3.6).
Figure 3.6 Using a RequiredFieldValidator with a DropDownList control.
Example 3.8. ShowInitialValue.aspx
<%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) If Page.IsValid Then lblResult.Text = dropFavoriteColor.SelectedValue End If End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server"> <title>Show Initial Value</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label id="lblFavoriteColor" Text="Favorite Color:" AssociatedControlID="dropFavoriteColor" Runat="server" /> <br /> <asp:DropDownList id="dropFavoriteColor" Runat="server"> <asp:ListItem Text="Select Color" Value="none" /> <asp:ListItem Text="Red" Value="Red" /> <asp:ListItem Text="Blue" Value="Blue" /> <asp:ListItem Text="Green" Value="Green" /> </asp:DropDownList> <asp:RequiredFieldValidator id="reqFavoriteColor" Text="(Required)" InitialValue="none" ControlToValidate="dropFavoriteColor" Runat="server" /> <br /><br /> <asp:Button id="btnSubmit" Text="Submit" Runat="server" OnClick="btnSubmit_Click" /> <hr /> <asp:Label id="lblResult" Runat="server" /> </div> </form> </body> </html>
The first list item displayed by the DropDownList control displays the text "Select Color". If you submit the form without selecting a color from the DropDownList control, then a validation error message is displayed.
Notice that the RequiredFieldValidator control includes an InitialValue property. The value of the first list from the DropDownList control is assigned to this property.