Powered by Blogger.

Sunday, February 23, 2014

RangeValidator example 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

- ValidationGroup example in Asp.Net

- How to get distinct data from tables in sql server 2008?

- Get user control value in aspx page 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 menimum 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>
    <form id="form1" runat="server">
        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"
        <br />
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" />

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