Checking for a Range of Values: The RangeValidator ControlYou can use the RangeValidator control to check whether the value of a form field falls between a minimum and maximum value. The minimum and maximum values can be dates, numbers , currency amounts, or strings. All the properties and methods of this control are listed in Table 3.4. Table 3.4. RangeValidator Properties, Methods, and Events
You can use RangeValidator , for example, to check whether a form field contains a date that falls within a certain range. The page in Listing 3.15 checks whether the date entered is greater or equal to today's date, but less than three months in the future. Listing 3.15 RangeValidator.aspx<Script Runat="Server"> Sub Page_Load valgMeetingDate.MinimumValue = Now.Date valgMeetingDate.MaximumValue = Now.Date.AddMonths( 3 ) End Sub Sub Button_Click( s As Object, e As EventArgs ) If IsValid Then Response.Redirect( "ThankYou.aspx" ) End If End Sub </Script> <html> <head><title>RangeValidator.aspx</title></head> <body> <form Runat="Server"> Choose a meeting date in the next three months: <br> <asp:TextBox id="txtMeetingDate" Columns="10" Runat="Server"/> <asp:RangeValidator ID="valgMeetingDate" ControlToValidate="txtMeetingDate" Display="Dynamic" Text="Date must be in the next 3 months!" Type="Date" 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. The Page_Load subroutine in Listing 3.15 assigns values to the MinimumValue and MaximumValue properties of the RangeValidator control. Today's date is assigned to the MinimumValue property, and a date three months in the future is assigned to the MaximumValue property. If you need to detect whether a value entered into a form field falls within a certain range determined by the values of other form fields, you should use the CompareValidator rather than the RangeValidator . The CompareValidator , unlike RangeValidator , enables you to compare the values of different controls. For example, the page in Listing 3.16 contains three TextBox controls. You must enter a number greater than or equal to the value entered into the first control, and less than or equal to the value entered into the last control; otherwise, you receive an error. Listing 3.16 CompareValidatorRange.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>CompareValidatorRange.aspx</title></head> <body> <form Runat="Server"> Minimum Value: <asp:TextBox id="txtMinNumber" Runat="Server"/> <p> Maximum Value: <asp:TextBox id="txtMaxNumber" Runat="Server"/> <p> Value: <asp:TextBox id="txtRangeNumber" Runat="Server"/> <asp:CompareValidator ControlToValidate="txtRangeNumber" ControlToCompare="txtMinNumber" Display="Dynamic" Text="Number must be greater than minimum value!" Operator="GreaterThan" Type="Integer" Runat="Server" /> <asp:CompareValidator ControlToValidate="txtRangeNumber" ControlToCompare="txtMaxNumber" Display="Dynamic" Text="Number must be less than maximum value!" Operator="LessThan" Type="Integer" 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. |