Nitish
Nitish

Reputation: 14113

Telerik MVC DatePicker - Disable all future dates

I have a Telerik MVC DatePicker :

<%: Html.Telerik().DatePickerFor(model => model.Date)
                         .TodayButton()
                         .Max(DateTime.Now)
                         .HtmlAttributes(new { @onkeypress = "return allowNumbersWithDot(event);", @onpaste = "return false;" })
                        %>  

By setting Max property, I get dates only till current date and all future dates are hidden. What I what is, enable all dates till current date and disable all future dates. i.e. user can see all dates but can select only till the current date.

Upvotes: 1

Views: 4245

Answers (2)

Iftekher
Iftekher

Reputation: 31

Minimum Date: Use it to set the smallest date, which the user can select.

<%= Html.Telerik().DatePicker()
                  .Name("DatePicker")
                  .MinDate(new DateTime(1900,1,1)))
%>

Maximum Date: Use it to set the biggest date, which the user can select.

<%= Html.Telerik().DatePicker()
                  .Name("DatePicker")
                  .MaxDate(new DateTime(2100,1,1)))
%>

Good Luck!

Upvotes: 2

Jeff Fritz
Jeff Fritz

Reputation: 9861

The Max property blocks the selection and appearance of all dates after the value set. To get around this, try formatting your DatePicker with a client event handler like:

@Html.Telerik().DatePickerFor(model => model.Date).ClientEvents(e => e.OnChange("onChange"))

and then add your validation with something like this:

function onChange(e) {
    var endDate = new Date(); // Your specific date
    if (e.value > endDate) {
        if (e.previousValue === null) {
            $(this).data("tDatePicker").value(null);
        }
        e.preventDefault();
    }
}

More information about this technique can be found in the Telerik support forums at: http://www.telerik.com/community/forums/aspnet-mvc/datepicker/datepicker---prevent-future-date-selection.aspx

Upvotes: 2

Related Questions