Validate a date using RangeValidator in asp.net c#

Validate a date using RangeValidator in asp.net 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.

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

Asp.net Related Post List:







    Comments

    Popular posts from this blog