Checking for a Range of Values: The RangeValidator Control


Checking for a Range of Values: The RangeValidator Control

You 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

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 control when the Text property is not set.

IsValid

Has the value True when the validation check succeeds and False otherwise .

MaximumValue

Specifies the maximum value in the range of permissible values.

MinimumValue

Specifies the minimum value in the range of values.

Text

Sets the error message displayed by the control.

Type

Gets or sets the data type to use when comparing values. Possible values are Currency , Date , Double , Integer , and String .

Methods

Description

Validate

Performs validation and updates the IsValid property.

Events

Description

None

 

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.



ASP.NET Unleashed
ASP.NET 4 Unleashed
ISBN: 0672331128
EAN: 2147483647
Year: 2003
Pages: 263

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net