Page

ASP.NET RangeValidator - How to validate date range

<%@ Page Language="C#" %>  
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  
<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: " +  
            Calendar1.SelectedDate.ToLongDateString();  
    }  
</script>  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>asp.net RangeValidator: how to validate date range (Validation Data Type Date)</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h2 style="color:Navy">RnageValidator: Date Range</h2>  
        <asp:Label ID="Label1" runat="server">  
        </asp:Label>  
        <br /><br />  
        <asp:TextBox ID="TextBox1" runat="server">  
        </asp:TextBox>  
        <asp:RequiredFieldValidator   
            ID="RequiredFieldValidator1"   
            runat="server"   
            ControlToValidate="TextBox1"  
            Text="*"  
            >  
        </asp:RequiredFieldValidator>  
        <br />  
        <b>Arrival Date</b>  
        <asp:Calendar   
            ID="Calendar1"   
            runat="server"  
            OnSelectionChanged="Calendar1_SelectionChanged"  
            >  
        </asp:Calendar>  
        <asp:RangeValidator ID="RangeValidator1" runat="server">  
        </asp:RangeValidator>  
        <br />  
        <asp:Button   
             ID="Button1"   
             runat="server"   
             Text="Submit Arrival Date"   
             OnClick="Button1_Click"   
             />  
    </div>  
    </form>  
</body>  
</html>  

No comments:

Post a Comment