- Chapter 3: Performing Form Validation with Validation Controls
- 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
Requiring Fields: The RequiredFieldValidator Control
You use RequiredFieldValidator in a Web form to check whether a control has a value. Typically, you use this control with a TextBox control. However, nothing is wrong with using RequiredFieldValidator with other input controls such as RadioButtonList. All the properties and methods of this control are listed in Table 3.1.
Table 3.1 RequiredFieldValidator Properties, Methods, and Events
Properties |
Description |
ControlToValidate |
Specifies the ID of the control that you want to validate. |
Display |
Sets how the error message contained in the Text property is displayed. Possible values are Static, Dynamic, and None; the default value is Static. |
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. |
ErrorMessage |
Specifies the error message that is displayed in the ValidationSummary control. This error message is displayed by the validation control when the Text property is not set. |
InitialValue |
Gets or sets the initial value of the control specified by the ControlToValidate property. |
IsValid |
Has the value True when the validation check succeeds and False otherwise. |
Text |
Sets the error message displayed by the control. |
Validate |
Performs validation and updates the IsValid property. |
Events |
Description |
None |
|
The page in Listing 3.1, for example, contains two TextBox controls named txtUsername and txtComments. Each TextBox control has a RequiredFieldValidator control associated with it. If you attempt to submit the form without entering data into both TextBox controls, you get an error.
Listing 3.1 RequiredFieldValidator.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>RequiredFieldValidator.aspx</title></head> <body> <form Runat="Server"> Username: <br><asp:TextBox ID="txtUsername" Runat="Server" /> <asp:RequiredFieldValidator ControlToValidate="txtUsername" Text="You must enter a username!" Runat="Server" /> <p> Comments: <br> <asp:TextBox id="txtComments" TextMode="MultiLine" Runat="Server"/> <asp:RequiredFieldValidator ControlToValidate="txtComments" Text="You must enter some comments!" Runat="Server" /> <p> <asp:Button Text="Submit" OnClick="Button_Click" Runat="Server"/> </form> </body> </html>
If you do not enter any text into the TextBox controls and you submit the form, the RequiredFieldValidator controls display error messages, and the IsValid property has the value False. Otherwise, the Button_Click subroutine automatically redirects you to a page named ThankYou.aspx.
When performing form validation, you can check the IsValid property for each Validation control to check whether each form field was completed successfully. Alternatively, you can simply check the IsValid property of the page. The Page.IsValid property is True only if the IsValid property is True for every Validation control on the page.
The RequiredFieldValidator controls are associated with the TextBox controls through the ControlToValidate property. For example, the first RequiredFieldValidator control is associated with the txtUsername control because its ControlToValidate property has the value txtUsername.
The error message that each RequiredFieldValidator displays is determined by the Text property. By default, the text is displayed in a red font. You can determine precisely how the error message is formatted by using the formatting properties discussed in the final section of the preceding chapter, "Building Forms with Web Server Controls."
If you want to display a blue error message using a Script font, for example, you would declare RequiredFieldValidator with the following properties:
<asp:RequiredFieldValidator ForeColor="Blue" Font-Name="Script" ControlToValidate="txtUsername" Text="You must enter a username!" Runat="Server" />
You also can control how the error messages appear by modifying the Display property. By default, screen real estate is reserved for an error message, even if the error message is not displayed. However, if you assign the value Dynamic to the Display property, the error message pushes away any content surrounding it.
The page in Listing 3.2, for example, contains two RequiredFieldValidator controls. The first RequiredFieldValidator control has its Display property set to Static; the second has its Display property set to Dynamic.
Listing 3.2 RequiredFieldValidatorDisplay.aspx
<html> <head><title>RequiredFieldValidatorDisplay.aspx</title></head> <body> <form Runat="Server"> Field 1: <br><asp:TextBox id="txtField1" Runat="Server" /> <asp:RequiredFieldValidator ControlToValidate="txtField1" Text="You must enter a value for field1!" Runat="Server" /> Here is Some Text <p> Field 2: <br> <asp:TextBox id="txtField2" Runat="Server"/> <asp:RequiredFieldValidator ControlToValidate="txtField2" Text="You must enter a value for field2!" Display="Dynamic" Runat="Server" /> Here is Some Text <p> <asp:Button Text="Submit" Runat="Server"/> </form> </body> </html>
The text, Here is Some Text, is written next to each RequiredFieldValidator. Notice how the text after the first RequiredFieldValidator control is pushed to the right (see Figure 3.2).
Figure 3.2. The difference between Dynamic and Static.
Comparing to an Initial Value
You also can use RequiredFieldValidator to check whether a user has entered a value other than an initial value. You might want to display a sample of the type of input that a user should enter into a form field, but require a different value to be entered into the form field before the form is submitted.
For example, you might want to display the text Enter Some Text within a text box. However, you would not want the user to actually submit the value Enter Some Text when the form is submitted.
The page in Listing 3.3 demonstrates how to use the InitialValue property of the RequiredFieldValidator control.
Listing 3.3 RequiredFieldValidatorInitialValue.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>RequiredFieldValidatorInitialValue.aspx</title></head> <body> <form Runat="Server"> Comments: <br> <asp:TextBox id="txtComments" TextMode="MultiLine" Text="Enter Some Text" Runat="Server"/> <asp:RequiredFieldValidator ControlToValidate="txtComments" Text="You must enter some comments!" InitialValue="Enter Some Text" Runat="Server" /> <p> <asp:Button Text="Submit" OnClick="Button_Click" Runat="Server"/> </form> </body> </html>
When the form in Listing 3.3 is first requested, the text Enter Some Text appears in the TextBox control. If you attempt to submit the form without changing this value, an error is displayed.