Range Validator
The RangeValidator control is utilized to check the input control value is inside a predetermined reach or not. We can say the info values should be between two determined ranges. It has a minimum and maximum values. They are mostly used to validate Pincode and mobile no.
The minimum value for validating mobile no. - 1111111111
The maximum value for validating mobile no. - 9999999999
Type - double
Syntax:
<asp:RangeValidator ID=“RangeValidator1“ runat=“server“ ErrorMessage=“RangeValidator“></asp:RangeValidator>
Example:
<asp:RangeValidator ID="class" runat="server" ControlToValidate="txtclass"
ErrorMessage="Enter your class (4 - 12)" MaximumValue="12"
MinimumValue="4" Type="Integer">
</asp:RangeValidator>
Range Validator Properties
-
Accesskey: It is used to set the keyboard’s shortcut.
-
TabIndex: It is used to tab order of the control.
-
BackColor: It is used to set the control's background color.
-
BorderColor: It is used to set the control's border color.
-
BorderWidth: It is used to set the width of the border.
-
Font: It is used to set the control text’s font.
-
ForeColor: It sets the control’s text color.
-
Text: It is used to set the text to be displayed for the control.
-
ToolTip: It displays the hovered control text by the mouse.
-
Visible: It sets the control’s visibility on the form.
-
Height: It is used to set the height of the control.
-
Width: It is used to set the width of the control.
-
ControlToCompare: It extracts the ID of the control for comparison.
-
ErrorMessage: It displays an error message in case of validation failure.
-
Type: It is used to set the data type of the control value.
-
MaximumValue: It is used to set the upper bound of the range.
- MinimumValue: It is used to set the lower bound of the range.
Range Validator Example
Consider the following example that will assist you in understanding the idea of range validator better:
Example 1:
Range.aspx
<%@ Page Language="C#" %>
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta charset="utf-8" />
<title></title>
</head>
<body>
<table>
<tr>
<td style="text-align: right;">
Name :
</td>
<td style="text-align: left;">
<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td style="text-align: right;">
Mobile:
</td>
<td style="text-align: left;">
<asp:TextBox ID="txtmobile" runat="server" ForeColor="Red" MaxLength="10"></asp:TextBox>
<asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="txtmobile" ErrorMessage="Invalid Mobile Number !" MaximumValue="9999999999" MinimumValue="1111111111" Type="Double"></asp:RangeValidator>
</td>
</tr>
<tr>
<td style="text-align: right;">
Pincode :
</td>
<td style="text-align: left;">
<asp:TextBox ID="txtpincode" runat="server" ForeColor="Red" MaxLength="6"></asp:TextBox>
<asp:RangeValidator ID="RangeValidator2" runat="server" ControlToValidate="txtpincode" ErrorMessage="Invalid Pincode !" MaximumValue="999999" MinimumValue="111111" Type="Integer"></asp:RangeValidator>
</td>
</tr>
<tr>
<td>
</td>
<td style="text-align: left;">
<asp:Button ID="btnSave" runat="server" Text="SAVE" style="font-weight: 700;" />
</td>
</tr>
</table>
<asp:RangeValidator ID="RangeValidator3" runat="server" ErrorMessage="RangeValidator"></asp:RangeValidator>
</body>
</html>
Output:


Example 2:
Range.aspx
<%@ Page Language="C#"%>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
height: 80px;
}
.auto-style2 {
width: 100%;
}
.auto-style3 {
width: 85px;
}
.auto-style4 {
margin-left: 75px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="auto-style1">
<p class="auto-style4">Enter value between 1 and 200<br /></p>
<table class="auto-style2">
<tr>
<td class="auto-style3">
<asp:Label ID="Label2" runat="server" Text="Enter a value"></asp:Label>
</td>
<td>
<asp:TextBox ID="userInput" runat="server"></asp:TextBox>
<asp:RangeValidator
ID="RangeValidator1"
runat="server"
ControlToValidate="userInput"
ErrorMessage="Enter value in the specified range"
ForeColor="Red"
MaximumValue="199"
MinimumValue="1"
SetFocusOnError="True"
Type=" Integer"
></asp:RangeValidator>
</td>
</tr>
<tr>
<td class="auto-style3"></td>
<td>
<br />
<asp:Button ID="Button2" runat="server" Text="Save" />
</td>
</tr>
</table>
<br />
<br />
</div>
</form>
</body>
</html>
Output:

FAQs
-
What do you understand by tracing in ASP.NET?
Tracing helps in displaying the application’s issues at the runtime. By default, tracing is disabled. The execution path of the application can be seen using the debug statement. We can also access and manipulate trace messages programmatically.
-
What do you understand by the term View state.
View State is the technique to save the page's value and control between round trips. It is a Page-Level State Management procedure. View State is turned on by default and serializes the data in each control on the page whether or not it is utilized during a post-back.
-
Explain caching in ASP.NET.
Caching is used to provide results to the users depending on their request. The admin needs to recreate the pages frequently depending on the user requests. Cache stores the output generated by a page in the memory, and this cache serves the users later. There are three types of caching as follows: Page caching, Fragment Caching, and Data Caching.
Key Takeaways
This blog taught us about one of the ASP.Net validation,i.e., Compare Validator. The user’s input must be validated before sending it across different application layers. The RangeValidator control is utilized to check the input control value is inside a predetermined reach or not.
Apart from this, you can also expand your knowledge by referring to these articles on Features Of ASP Net and ASP Full Form.
If you are a beginner and are keen on learning different fields related to computer subjects such as web development, DSA, or competitive programming, you can follow our guided path to get a good grip on such concepts.