RangeValidator - ( ASP.NET Validation )
ASP.NET validation controls allow you to check the input of the user and ensure that it matches your requirements. One such control is the RangeValidator
, which checks if a user input value is within a specified range. In this page, we will discuss the RangeValidator
control in detail.
Syntax
The syntax for RangeValidator
control is as follows:
<asp:RangeValidator
ID="RangeValidator1"
runat="server"
ControlToValidate="controlid"
MaximumValue="maxvalue"
MinimumValue="minvalue"
ErrorMessage="errormessage"
Display="dynamic/static/none">
</asp:RangeValidator>
ID
: ID of the control.runat
: specifies whether the control is server-side or client-side.ControlToValidate
: ID of the control to be validated.MaximumValue
: the largest possible value.MinimumValue
: the smallest possible value.ErrorMessage
: error message to be displayed if validation fails.Display
: specifies when the validation error message is displayed.
Example
Here is an example of how to use the RangeValidator
control in a form to validate a user's input:
<asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
<asp:RangeValidator
ID="RangeValidator1"
runat="server"
ControlToValidate="txtAge"
MaximumValue="100"
MinimumValue="18"
ErrorMessage="Age should be between 18 and 100 years."
Display="Dynamic">
</asp:RangeValidator>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
In this example, we have a TextBox
control with an ID of txtAge
. We have used the RangeValidator
control to validate the user's input in the txtAge
control. The MaximumValue
has been set to 100, while the MinimumValue
has been set to 18. The error message will be displayed dynamically using the Display
property, and the ErrorMessage
will show if the input value is not within the specified range.
Output
When the user submits the form, if the input value in the control being validated is not within the specified range, the RangeValidator
control displays the error message.
Explanation
The RangeValidator
control checks if the value of the input control falls within the specified range or not. If the value falls within the specified range, the control is valid. Otherwise, the error message is displayed.
Use
You can use RangeValidator
control to validate a range of input types such as numbers, dates, and currency values.
Important Points
RangeValidator
is useful to ensure the user's input values are within the specified range.- The
RangeValidator
control has properties likeMinimumValue
andMaximumValue
which are used to validate input values. - The
ErrorMessage
property specifies the error message that is displayed if the user input is not within the specified range.
Summary
In this page, we discussed the RangeValidator
control and how to use it to validate user input value. We covered the syntax, example, output, explanation, use, and important points of RangeValidator
. By using this control, you can easily restrict user input to specified ranges to ensure accurate data input.