20 November 2013

RangeValidator of Validation Controls in Asp.Net.

In this post we will see an example of how to use RangeValidator in Asp.Net.
There are different types of validation controls are provided by Asp.Net. Check this article for more information.
You can also check my previous post on RequiredFieldValidator example in Asp.Net.

- RangeValidator: Here validation succeeds if the input control contains a value within a specific numeric, alphabetic, or date range.

- To use RangeValidator, drag and drop a RangeValidator from Toolbox.

Below are the property need to set:

ControlToValidate: This property field needs to be set as on which field you want to validate.

ErrorMessage: This property will show what ever error message you will give.

MaximumValue: Indicates what will be the maximum value allowed.

MinimumValue: Indicates what will be the minimum value allowed.

Type: Indicates what will be the type. The value can be Integer, Currency, Date, Double or String.

Below is the full HTML code :

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ValidatorExamples.aspx.cs"
    Inherits="ValidatorExamples" %>

<!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 runat="server">
    <title>RangeValidator example in Asp.Net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Enter Age:<asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
        <br />
        <asp:RangeValidator ID="RangeValidator1" runat="server" ErrorMessage="Age should be between 10 to 90!"
            ForeColor="Red" ControlToValidate="txtAge" MaximumValue="90" MinimumValue="10"
            Type="Integer"></asp:RangeValidator>
        <br />
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
    </div>
    </form>
</body>
</html>

If you try to submit enter any value in the Textbox which is not between the minimun value and  maximum value set then you will get the error message as shown in the figure below:





No comments:

Post a Comment