Telerik MVC DatePicker - Disable all future dates

蹲街弑〆低调 提交于 2019-12-11 12:55:12

问题


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.


回答1:


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




回答2:


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!



来源:https://stackoverflow.com/questions/16216788/telerik-mvc-datepicker-disable-all-future-dates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!