Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I need to show the user only the current date along with 5 past dates. Rest dates/month should be disabled. Please help to achieve this using range validator in html & .net

What I have tried:

ASP.NET
<td class="auto-style1" align="left" style="vertical-align: top;">
    Entry Date
</td>
<td align="left" style="vertical-align: top;" class="auto-style2">
    <asp:TextBox ID="txtFromDate" runat="server" CssClass="textbox" TabIndex="6" Width="100px"></asp:TextBox>
    <cc1:CalendarExtender ID="CEPostingFromDate" runat="server" TargetControlID="txtFromDate">
    </cc1:CalendarExtender>
    <cc1:MaskedEditExtender ID="MaskedEditExtender1" runat="server" 
        CultureAMPMPlaceholder="" 
        CultureCurrencySymbolPlaceholder="" 
        CultureDateFormat="" 
        CultureDatePlaceholder="" 
        CultureDecimalPlaceholder="" 
        CultureThousandsPlaceholder="" 
        CultureTimePlaceholder="" 
        Enabled="True" 
        Mask="99/99/9999" MaskType="Date" 
        TargetControlID="txtFromDate" 
        UserDateFormat="MonthDayYear"
    >
    </cc1:MaskedEditExtender>
    <cc1:TextBoxWatermarkExtender ID="TextBoxWatermarkExtender1" runat="server" 
        Enabled="True" 
        TargetControlID="txtFromDate" 
        WatermarkText="MM/DD/YYYY"
    >
    </cc1:TextBoxWatermarkExtender>
    <asp:RangeValidator ID="RangeValidator2" runat="server" 
        ControlToValidate="txtFromDate" 
        ErrorMessage="Choose only the enabled dates" 
        MaximumValue="01/10/2023" 
        MinimumValue="01/05/2023" 
        Type="Date"
    >*</asp:RangeValidator>
    <cc1:ValidatorCalloutExtender ID="ValidatorCalloutExtender2" runat="server" TargetControlID="RangeValidator2">
    </cc1:ValidatorCalloutExtender>
</td>
Posted
Updated 9-Jan-23 21:41pm
v2

1 solution

You'll need to use code to set the properties:
ASP.NET
<asp:RangeValidator ID="RangeValidator2" runat="server" 
    ControlToValidate="txtFromDate" 
    ErrorMessage="Choose only the enabled dates" 
    Type="Date"
    OnInit="RangeValidator2_Init"
>*</asp:RangeValidator>
C#
protected void RangeValidator2_Init(object sender, EventArgs e)
{
    RangeValidator validator = (RangeValidator)sender;
    DateTime today = DateTime.Today;
    validator.MaximumValue = today.ToString("yyyy/MM/dd");
    validator.MinimumValue = today.AddDays(-5).ToString("yyyy/MM/dd");
}
NB: Pay attention to the documentation:
If you specify ValidationDataType.Date for the BaseCompareValidator.Type property without programmatically setting the culture for the application, you should use a culture-neutral format, such as YYYY/MM/DD, for the MaximumValue and MinimumValue properties. Otherwise, the date may not be interpreted correctly.

Edit: To limit the dates available in the calendar, set its properties in the same way:
ASP.NET
<cc1:CalendarExtender ID="CEPostingFromDate" runat="server" 
    TargetControlID="txtFromDate"
    OnInit="CEPostingFromDate_Init"
/>
C#
protected void CEPostingFromDate_Init(object sender, EventArgs e)
{
    CalendarExtender extender = (CalendarExtender)sender;
    DateTime today = DateTime.Today;
    extender.StartDate = today.AddDays(-5);
    extender.EndDate = today;
}
Ajax control toolkit Calendar example with demo[^]
 
Share this answer
 
v2
Comments
Jaichithra 10-Jan-23 5:19am    
Hi Richard Deeming,
I tried your code, yet the whole calendar is only getting displayed. I want to display only the current date and past 5 dates.
Richard Deeming 10-Jan-23 5:21am    
Your question was about limiting the valid dates on the range validator. If you want to change the enabled dates in the calendar extender, then you'll need to set the properties on that control in a similar way.
Jaichithra 10-Jan-23 9:06am    
Thank you @Richard Deeming. Your solution worked.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900