RangeValidator For Date To allow Only Past One Month


Sometimes we need to allow date in textbox only from past one month for that follow this code

First place a scriptmanager in webform and write following code

Add Registration Tag:

<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>

Code in .aspx Page:

<asp:TextBox ID="TextBox1" runat="server" Visible="False"></asp:TextBox>
<asp:CalendarExtender ID="TextBox1_CalendarExtender" runat="server"
Enabled="True" TargetControlID="TextBox1">
</asp:CalendarExtender>
<asp:RangeValidator ID="RangeValidator1" runat="server"
ErrorMessage="Select Date From Past One Month Only" ControlToValidate="TextBox1" Type="Date"></asp:RangeValidator>
<asp:ValidatorCalloutExtender ID="RangeValidator1_ValidatorCalloutExtender"
runat="server" Enabled="True" TargetControlID="RangeValidator1">
</asp:ValidatorCalloutExtender>

Code in .aspx.cs file:

protected void Page_Load(object sender, EventArgs e)
{
RangeValidator1.MaximumValue = RangeValidator2.MaximumValue = System.DateTime.Now.ToShortDateString();
RangeValidator1.MinimumValue = RangeValidator2.MinimumValue = System.DateTime.Now.AddDays(-30).ToShortDateString();
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s