Completing a Web Form in ASP.NET - How to use the required field validator
(Page 2 of 5 )
To use the required field validator, you set the properties shown in the table at the top of figure 2-13. These are the properties that are used by all the validators.
To start, you associate the validation control with a specific input control on the form through its ControlToValidate property. Then, when the focus leaves the input control or the user clicks on a button whose CausesValidation property is set to True, the validator checks whether a value has been entered into the input control. If not, the message in the ErrorMessage property is displayed.
When an error occurs, the Display property of the validation control determines how the message in the ErrorMessage property is displayed. When you use flow layout, Dynamic usually works the best for this property. If you use a validation summary control as explained in chapter 7, though, you can change this property to None.
If you look at the aspx code in this figure, you can see how the properties are set for the two required field validators that are shown in the previous figure. The first one validates the text box named txtInterestRate. The second one validates the text box named txtYears. This aspx code will be added after the end tag for the table in the code in figure 2-11.
How to use the range validator
The range validator lets you set the valid range for an input value. To use this control, you set the properties in the first table in figure 2-13, plus the properties in the second table. In particular, you set the minimum and maximum values for an input value.
For this control to work correctly, you must set the Type property to the type of data you’re testing for. Because the interest rate entry can have decimal positions, for example, the Type property for the first range validator is set to Double. In contrast, because the year entry should be a whole number, the Type property for the second range validator is set to Integer. You can see how all of the properties for the two range validators are set by reviewing the aspx code.
Common validation control properties
Property | Description |
ControlToValidate | The ID of the control to be validated. |
Display | Determines how an error message is displayed. Specify Static to allocate space for the message in the page layout, Dynamic to have the space allocated when an error occurs, or None to display the errors in a validation summary control. |
ErrorMessage | The message that’s displayed in the validation control when the validation fails. |
Additional properties of a range validator
Property | Description |
Maximum | The maximum value that the control can contain. |
Minimum | The minimum value that the control can contain. |
Type | The data type to use for range checking (String, Integer, Double, Date, or Currency). |
The aspx code for the validation controlson the Future Value form
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate= "txtInterestRate" Display="Dynamic"
ErrorMessage="Interest rate is required.">
</asp:RequiredFieldValidator>
<asp:RangeValidator ID="RangeValidator1" runat="server"
ControlToValidate= "txtInterestRate" Display="Dynamic"
ErrorMessage="Interest rate must range from 1 to 20."
MaximumValue="20" MinimumValue="1" Type="Double">
</asp:RangeValidator><br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtYears" Display="Dynamic"
ErrorMessage="Number of years is required.">
</asp:RequiredFieldValidator>
<asp:RangeValidator ID="RangeValidator2" runat="server"
ControlToValidate="txtYears" Display="Dynamic"
ErrorMessage="Years must range from 1 to 45."
MaximumValue="45" MinimumValue="1" Type="Integer">
</asp:RangeValidator>
Figure 2-13. How to use the required field and range validators
Description
- The required field validator is typically used with text box controls, but can also be used with list controls.
- The range validator tests whether a user entry falls within a valid range.
- If the user doesn’t enter a value into the input control that a range validator is associated with, the range validation test passes. Because of that, you should also provide a required field validator if a value is required.
Next: How to add code to a form >>
More ASP.NET Articles
More By Murach Publishing
|
This article is excerpted from the book Murach's ASP.NET 2.0 Web Programming with VB2005, written by Doug Lowe (Murach, 2006; ISBN: 1890774324). Check it out today at your favorite bookstore. Buy this book now.
|
|