- Using Client-side Validation
- Requiring Fields: The RequiredFieldValidator Control
- Validating Expressions: The RegularExpressionValidator Control
- Comparing Values: The CompareValidator Control
- Checking for a Range of Values: The RangeValidator Control
- Summarizing Errors: The ValidationSummary Control
- Performing Custom Validation: The CustomValidator Control
- Disabling Validation
- Summary
Summarizing Errors: The ValidationSummary Control
Imagine that you have a form with 50 form fields. If you use only the Validation controls discussed in the previous sections of this chapter to display errors, seeing an error message on the page might be difficult. For example, you might have to scroll down to the 48th form field to find the error message.
Fortunately, Microsoft includes a ValidationSummary control with the Validation controls. You can use this control to summarize all the errors at the top of a page, or wherever else you want. All the properties and methods of this control are listed in Table 3.5.
Table 3.5 ValidationSummary Properties, Methods, and Events
Properties |
Description |
DisplayMode |
Sets the formatting for the error messages displayed by the control. Possible values are BulletList, List, and SingleParagraph. |
EnableClientScript |
Enables or disables client-side form validation. This property has the value True by default. |
Enabled |
Enables or disables both server and client-side validation. This property has the value True by default. |
HeaderText |
Sets the text that is displayed at the top of the summary. |
ShowMessageBox |
When True, displays error messages in a pop-up message box. |
ShowSummary |
Enables or disables the summary of error messages. |
Methods |
Description |
None |
|
Events |
Description |
None |
|
The page in Listing 3.17 illustrates how you can use the ValidationSummary control to display a summary of errors (see Figure 3.3).
Figure 3.3 Summarizing errors with the ValidationSummary control.Listing 3.17 ValidationSummary.aspx
<Script Runat="Server"> Sub Button_Click( s As Object, e As EventArgs ) If IsValid Then Response.Redirect( "ThankYou.aspx" ) End If End Sub </Script> <html> <head><title>ValidationSummary.aspx</title></head> <body> <form Runat="Server"> <asp:ValidationSummary HeaderText="There are problems with the following form fields:" Runat="Server" /> <p> First Name: <br> <asp:TextBox ID="txtFirstname" Runat="Server" /> <asp:RequiredFieldValidator ID="reqVal1" ControlToValidate="txtFirstname" Text="You must enter a first name!" ErrorMessage="First Name" Runat="Server" /> <p> Last Name: <br> <asp:TextBox ID="txtLastname" Runat="Server" /> <asp:RequiredFieldValidator ControlToValidate="txtLastname" Text="You must enter a last name!" ErrorMessage="Last Name" Runat="Server" /> <p> Occupation: <br> <asp:TextBox ID="txtOccupation" Runat="Server" /> <asp:RequiredFieldValidator Text="You must enter an occupation!" ControlToValidate="txtOccupation" ErrorMessage="Occupation" Runat="Server" /> <p> <asp:Button Text="Submit" OnClick="Button_Click" Runat="Server"/> </form> </body> </html>
The C# version of this code can be found on the CD-ROM.
Notice how each Validation control is assigned an error message with the ErrorMessage property. These error messages are displayed in the ValidationSummary control whenever a problem occurs with a form field.
Don't confuse the ErrorMessage and Text properties. Typically, you use the Text property of a Validation control to display an error message next to a form field, and you use the ErrorMessage property to display a message in the ValidationSummary control.
By default, the ValidationSummary control displays error messages in a bulleted list. However, you also have the option of displaying the messages in a nonbulleted list or within a single paragraph. To control how the ValidationSummary control formats its summary of errors, modify its DisplayMode property. Figure 3.4 shows how the ValidationSummary control displays error messages with each of the different settings of the DisplayMode property.
Figure 3.4 Different ValidationSummary display modes.Displaying Pop-Up Error Messages
Have you ever seen those obnoxious pop-up error messages that some sites use to report validation errors? You can provide these messages for your users, too!
You can enable the ValidationSummary control's ShowMessageBox property to display error messages in a dialog box. Listing 3.18 demonstrates how you can enable this property (see Figure 3.5 for the output).
NOTE
If you want to show the error message summary only in the pop-up dialog box and not on the page itself, set the ValidationSummary control's ShowSummary property to False.
Listing 3.18 ValidationSummaryPopUp.aspx
<Script Runat="Server"> Sub Button_Click( s As Object, e As EventArgs ) If IsValid Then Response.Redirect( "ThankYou.aspx" ) End If End Sub </Script> <html> <head><title>ValidationSummaryPopUp.aspx</title></head> <body> <form Runat="Server"> <asp:ValidationSummary ShowMessageBox="True" HeaderText="There are problems with the following form fields:" Runat="Server" /> <p> First Name: <br> <asp:TextBox ID="txtFirstname" Runat="Server" /> <asp:RequiredFieldValidator ControlToValidate="txtFirstname" Text="You must enter a first name!" ErrorMessage="First Name" Runat="Server" /> <p> Last Name: <br> <asp:TextBox ID="txtLastname" Runat="Server" /> <asp:RequiredFieldValidator ControlToValidate="txtLastname" Text="You must enter a last name!" ErrorMessage="Last Name" Runat="Server" /> <p> Occupation: <br> <asp:TextBox ID="txtOccupation" Runat="Server" /> <asp:RequiredFieldValidator Text="You must enter an occupation!" ControlToValidate="txtOccupation" ErrorMessage="Occupation" Runat="Server" /> <p> <asp:Button Text="Submit" OnClick="Button_Click" Runat="Server"/> </form> </body> </html>
The C# version of this code can be found on the CD-ROM.