Validate an integer using RangeValidator in asp.net c#

RangeValidator - Validate data type integer
RangeValidator control check whether inputted value is within a specified range. rangevalidator mainly uses four properties to validate input value range. they are ControlToValidate, MinimumValue, MaximumValue and Type (data type). Type property indicate the inputted control data type. there are five types of data rangvalidator can validate. those are string, integer, currency, double and date type data.

this example demonstrate integer (number/numeric) type data range validation. set minimum and maximum integer value to validate inputted value range. here we create a listbox which items value between 10 to 100. we specify the rangevalidator minimum value 40 and maximum value 60. so if user select a list item from range which value between 40 to 60 then he will pass the validation process. otherwise it will fail validation and rangevalidator generate an error message.
RangeValidatorDataTypeInteger.aspx

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

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "Number is valid: " +
            DropDownList1.SelectedItem.Text.ToString();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>asp.net RangeValidator: how to validate data type integer</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Navy">RangeValidator: Data Type Integer</h2>
        <asp:Label 
             ID="Label1" 
             runat="server"
             Font-Size="Large"
             ForeColor="SeaGreen"
             >
        </asp:Label>
        <br /><br />
        <b>Select a number between 40 to 60</b>
        <br />
        <asp:DropDownList 
            ID="DropDownList1"
            runat="server"
            BackColor="DodgerBlue"
            ForeColor="Snow"
            >
            <asp:ListItem>10</asp:ListItem>
            <asp:ListItem>20</asp:ListItem>
            <asp:ListItem>30</asp:ListItem>
            <asp:ListItem>40</asp:ListItem>
            <asp:ListItem>50</asp:ListItem>
            <asp:ListItem>60</asp:ListItem>
            <asp:ListItem>70</asp:ListItem>
            <asp:ListItem>80</asp:ListItem>
            <asp:ListItem>90</asp:ListItem>
            <asp:ListItem>100</asp:ListItem>
        </asp:DropDownList>
        <asp:RangeValidator 
            ID="RangeValidator1"
            runat="server"
            ControlToValidate="DropDownList1"
            Type="Integer"
            MinimumValue="40"
            MaximumValue="60"
            ErrorMessage="Select Number between 40 to 60"
            >
        </asp:RangeValidator>
        <br /><br />
        <asp:Button 
             ID="Button1" 
             runat="server" 
             Font-Bold="true"
             ForeColor="DodgerBlue"
             Text="Submit Number" 
             OnClick="Button1_Click" 
             />
    </div>
    </form>
</body>
</html>
Related asp.net examples