Validate a date using RangeValidator in c#

RangeValidator - Validate date range
RangeValidator control check whether input control value is within a specified range of values. using Type property we can tell rangevalidator of validating values data type as like integer, date, double, currency or string. rangevalidator converted input values specific data type before apply any comparison. by ErrorMessage property we can set rangevalidator validation fail error message text. rangevalidator control have two property MaximumValue and MinimumValue to specify validation range. rangevalidator ControlToValidate property value set the input control to validate. MinimumValue and MaximumValue specify the valid range of control inputted value. validation ignore if you not set a requiredfieldvalidator and user leave input field without entering any value.

this example show you how can we validate date range using rangevalidator control. rangevalidator control force user to select a date from calendar control between a specified date range.

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

<!DOCTYPE html>

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
        if (!this.IsPostBack)
            Label1.Font.Italic = true;
            Label1.Font.Bold = true;
            Label1.ForeColor = System.Drawing.Color.Crimson;
            Label1.Font.Size = FontUnit.Large;
            TextBox1.ForeColor = System.Drawing.Color.DodgerBlue;
            Button1.Font.Bold = true;
            Button1.ForeColor = System.Drawing.Color.DarkGreen;

            Calendar1.ForeColor = System.Drawing.Color.AliceBlue;
            Calendar1.BackColor = System.Drawing.Color.DodgerBlue;
            Calendar1.TitleStyle.BackColor = System.Drawing.Color.DarkGreen;
            Calendar1.DayHeaderStyle.BackColor = System.Drawing.Color.SeaGreen;
            Calendar1.SelectedDayStyle.BackColor = System.Drawing.Color.LightBlue;
            Calendar1.SelectedDayStyle.ForeColor = System.Drawing.Color.DarkGreen;
            Calendar1.BorderColor = System.Drawing.Color.DarkBlue;
            Calendar1.TodayDayStyle.BackColor = System.Drawing.Color.SkyBlue;
            Calendar1.SelectionMode = CalendarSelectionMode.Day;

            RangeValidator1.ControlToValidate = "TextBox1";
            RangeValidator1.Type = ValidationDataType.Date;
            RangeValidator1.MinimumValue = DateTime.Now.ToShortDateString();
            RangeValidator1.MaximumValue = DateTime.Now.AddDays(7).ToShortDateString();
            RangeValidator1.ErrorMessage = "Select date between today to next 7 day!";

    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        TextBox1.Text = Calendar1.SelectedDate.ToShortDateString();

    protected void Button1_Click(object sender, EventArgs e)
        Label1.Text = "Your arrival date is: " +

<html xmlns="">
<head id="Head1" runat="server">
    <title> RangeValidator: how to validate date range (Validation Data Type Date)</title>
    <form id="form1" runat="server">
        <h2 style="color:Navy">RnageValidator: Date Range</h2>
        <asp:Label ID="Label1" runat="server">
        <br /><br />
        <asp:TextBox ID="TextBox1" runat="server">
        <br />
        <b>Arrival Date</b>
        <asp:RangeValidator ID="RangeValidator1" runat="server">
        <br />
             Text="Submit Arrival Date" 
Related examples