This is a migrated thread and some comments may be shown as answers.

DatePicker - Prevent future date selection

10 Answers 1409 Views
Date/Time Pickers
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Ashok
Top achievements
Rank 1
Ashok asked on 25 Apr 2011, 10:02 PM
I am using Telerik DatePicker. Is there a way I can display whole month, but prevent future date selection.
e.g.: Current Month is April 2011. Calendar displays Full April 2011, however if today is Apr 25, user can only select Apr 25. Can't go beyond this date.

I tried .Max(), but this will only display today's date max.

10 Answers, 1 is accepted

Sort by
0
Georgi Krustev
Telerik team
answered on 26 Apr 2011, 12:04 PM
Hello Tejas,

 
You can try to achieve your goal with the following code snippet:

function onChange(e){
   var endDate = new Date("10/10/2000");
   if(e.value > endDate){
       e.preventDefault();
   }
}
As you can notice you should wire OnChange event and in the event handler you should call preventDefault() if the selected date is bigger than the allowed one.

Best regards,
Georgi Krustev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Ashok
Top achievements
Rank 1
answered on 27 Apr 2011, 03:47 PM
Hi Georgi,

function ReferredTo_OnChange(e) {
    var endDate = new Date();
    if (e.value > endDate) {
        e.preventDefault();
    }
}

Based on your sample if I try to select future date, calendar (textbox) shows: 12-31/1969 as default date.

1) Is there a way to grayout future date? If Today is "04-27-2011" than user can't select any future date (as they are greyedout) on calendar.

2) I have two date picker From and To. Is there a way I prevent date selection as follows:
    if "From Date" is 04-20-2011 then "To Date" can't be less than "From Date"?

3) Is there a way we can prevent so that user can't type date in text box and has to use date picker?
    
0
Ashok
Top achievements
Rank 1
answered on 29 Apr 2011, 02:26 PM
Any update on how to perform any of this?
0
Accepted
Georgi Krustev
Telerik team
answered on 03 May 2011, 04:31 PM
Hello Tejas,

Here is the correct way to stop selecting date bigger than "endDate":

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

And about the other questions:

#1: It will be hard to maintain "grayed" days through months navigation. Even you apply gray style to the days bigger than the "endDate" this style will be removed if the end user navigate to previous and then to the current month. One possible solution is to apply "t-other-month" class to the TD which contains date bigger than the "endDate". You can try to do this in the Load event handler. Just get calendar like so:
var calendar = $(this).data("tDatePicker").dateView.$calendar;
Then you just need to find required TDs and apply "t-other-month".

#2: On "From Date" change event just set min date of the "To Date". Use min() method to apply MinValue of the "To Date" datepicker.

#3: You can try to apply disabled="disabled" attribute to the HTML input element.

Regards,
Georgi Krustev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Ashok
Top achievements
Rank 1
answered on 03 May 2011, 04:33 PM
Thank you. This will surely help. We will take your advice make changes to the UI accrodingly.
0
Ashok
Top achievements
Rank 1
answered on 04 May 2011, 06:48 PM
When I try to use 





You can try to apply disabled="disabled" attribute to the HTML input element.


I get javascript error in IE7.
Can't move focus to the control because it is invisible, not enabled, or of a type that does not accept the focus!

I have also attached screen shot.
How do we resolve this?

0
Georgi Krustev
Telerik team
answered on 09 May 2011, 10:18 AM
Hello Tejas,

In order to avoid the aforementioned behavior I will suggest you apply readonly attribute instead of disabled:

$("#DatePicker").attr("readonly", true)

Regards,
Georgi Krustev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Ashok
Top achievements
Rank 1
answered on 09 May 2011, 03:45 PM
Hi Georgi,

Do you think this will work:
.InputHtmlAttributes(new { readonly = true })


OR do I have to do this with javascript (what function should I use for javascript)?
0
Accepted
Georgi Krustev
Telerik team
answered on 12 May 2011, 02:48 PM
Hello Tejas,

readonly is a modifier in C#. That is why you will need to use the suggested code like this:
.InputHtmlAttributes(new { @readonly = true })

Greetings,
Georgi Krustev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Ashok
Top achievements
Rank 1
answered on 13 May 2011, 07:48 PM
This helped. Thanks.
Tags
Date/Time Pickers
Asked by
Ashok
Top achievements
Rank 1
Answers by
Georgi Krustev
Telerik team
Ashok
Top achievements
Rank 1
Share this question
or