Table of contents
1.
Introduction
2.
Validation Controls in ASP.NET
3.
Range Validator
3.1.
Range Validator Properties
4.
Range Validator Example
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024

ASP.NET Range Validator

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Validation assumes to be a fundamental part of web applications. The client's input should be approved before sending it across various application layers. The validation controls execute show rationale to approve client input, information design, information type, and date range.

The validation is additionally partitioned into two classifications:

  1. Client-side validation: This is advantageous for clients as they get instant feedback. The primary benefit of this validation is that it keeps the page from being post-back to the server until the client-side validation is executed effectively. 
  2. Server-side validation: This validation is ideal according to an engineer's perspective as it won't fall flat.

 

ASP.NET validation guarantees both client-side and server-side validation. To start with, it works on the client-side validation and afterward on the server-side validation. The server validation will continuously run whether or not client validation is executed. 

Validation Controls in ASP.NET

An essential part of making ASP.NET website pages for client input is to make sure that the client's entered input is valid. The ASP.NET gives a bunch of validation controls that give a simple yet strong method for checking for mistakes and showing messages to the client.

Following are the six kinds of validation controls in ASP.NET alongside their working:  

  1. RequiredFieldValidation Control: It makes an input a required field.
     
  2. CompareValidator Control: It compares the value of one input to another. 
     
  3. RangeValidator Control: It is used to check whether the input entered by the user falls under the required range.
     
  4. RegularExpressionValidator Control: It ensures that the input value matches the specified pattern.
     
  5. CustomValidator Control: This allows a method to validate the entered value.
     
  6. ValidationSummary: It displays a report of all the validation errors that occurred on a web page.

 

This blog will discuss one of the validation controls, i.e., RangeValidator control, with a few examples.

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

  1. Accesskey: It is used to set the keyboard’s shortcut.
     
  2. TabIndex: It is used to tab order of the control.
     
  3. BackColor: It is used to set the control's background color.
     
  4. BorderColor: It is used to set the control's border color.
     
  5. BorderWidth: It is used to set the width of the border.
     
  6. Font: It is used to set the control text’s font.
     
  7. ForeColor: It sets the control’s text color.
     
  8. Text: It is used to set the text to be displayed for the control.
     
  9. ToolTip: It displays the hovered control text by the mouse.
     
  10. Visible: It sets the control’s visibility on the form.
     
  11. Height: It is used to set the height of the control.
     
  12. Width: It is used to set the width of the control.
     
  13. ControlToCompare: It extracts the ID of the control for comparison.
     
  14. ErrorMessage: It displays an error message in case of validation failure.
     
  15. Type: It is used to set the data type of the control value.
     
  16. MaximumValue: It is used to set the upper bound of the range.
     
  17. 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>
                    &nbsp;
                </td>
                <td style="text-align: left;">
                    <asp:Button ID="btnSave" runat="server" Text="SAVE" style="font-weight: 700;" />
                    &nbsp;
                </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

  1. 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.
     
  2. 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. 
     
  3. 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.

Live masterclass