-->

22 December 2019

Asp.Net RangeValidator Control

  Asp.Net CS By Example       22 December 2019



   The RangeValidator Control :
   In this post,we are leaning about ASP.NET RangeValidator Control. RangeValidator control is we can use when we want the user input in between any range of any values. Force to user enter value between a certain minimum and maximum value range. If associate control have no value then this control not works. It is use with RequiredFieldValidator control. We must set five properties when using this control:
Syntax:
<asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="txtAge" 
  ErrorMessage="Invalid Age" MinimumValue="18" MaximumValue="80" Type="Integer" />



The RangeValidator has six important properties:
Sr.No Property Description
1) ID Set the uniqe value to control to access in code-behind code.
2) ControlToValidate which control is check or The ID of the form field validated.
3) Text The error message displayed when validation fails.
4) MinimumValue The minimum value of the validation range.
5) MaximumValue The maximum value of the validation range.
6) Type The type of comparison to perform. Possible values are String, Integer,Double, Date, and Currency.

Code: RangeValidatorDemo.aspx

<%@ Page Language="C#" AutoEventWireup="true"  %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
     <style>
        .column
        {
            float: left;
            width: 30%;
            margin-left: 10px;
            background-color: white;
            border: solid 1px black;
            padding: 10px;
            width: 70%;
        }
        fieldset
        {
            background-color: #a9a9a9;
            border: solid 2px #d81a72;
            color:Black;
            width:70%;
        }
    </style>
</head>
<body style="width: 70%;">
    <form id="form1" runat="server" >
                <div class="column">
            <fieldset>
                <legend>Range Validator Demo</legend>
                <asp:Label ID="lblAge" Style="margin-right: 12px;" ValidationGroup="rv" AssociatedControlID="txtAge"
                    runat="server">Age:</asp:Label> 
                <asp:TextBox ID="txtAge" MaxLength="3" ValidationGroup="rv" runat="server"></asp:TextBox>
                <asp:RangeValidator ID="rvAge" ControlToValidate="txtAge" SetFocusOnError="true"
                    ErrorMessage="Plz Enter Age Between 0 to 150" ValidationGroup="rv" MinimumValue="0"
                    MaximumValue="150" Type="Integer" runat="server"></asp:RangeValidator>
                <asp:RequiredFieldValidator ID="rfvAge" ControlToValidate="txtAge" ErrorMessage="Enter Age"
                    ValidationGroup="rv" SetFocusOnError="true" runat="server"></asp:RequiredFieldValidator>
                <br />
                <asp:Button runat="server" ID="btnRangeValidate" ValidationGroup="rv" Text="Validate"
                    />
            </fieldset>
            
            </div>

    </form>
</body>
</html>

                

Output:

logoblog

Thanks for reading Asp.Net RangeValidator Control

Previous
« Prev Post