Solved

Calendar extender prevent selection of previous date

Posted on 2013-01-10
9
1,360 Views
Last Modified: 2013-01-25
I am trying to prevent people from selecting an earlier date than today.

I found this function that works great
   function checkDate(sender, args) {
        alert(sender._selectedDate);
        alert(new Date());
        if (sender._selectedDate < new Date()) {
            alert("You cannot select a day earlier than today!");
            sender._selectedDate = new Date();
            // set the date back to the current date
            sender._textbox.set_Value(sender._selectedDate.format(sender._format))
        }
    }
  <asp:CalendarExtender   OnClientDateSelectionChanged="checkDate"   Format="yyyy/MM/dd" ID="Tbx_targetDate_CE" runat="server" Enabled="True"
                                TargetControlID="Tbx_targetDate">

But the sender._selectedDate shows a day ealier so if i select the current date on the calendar I will get the alert "You cannot select a day earlier than today!");"
I need to be able to select the current day
0
Comment
Question by:arnololo123
  • 4
  • 4
9 Comments
 
LVL 21

Expert Comment

by:masterpass
ID: 38765223
Have a look at this approach. The logic is inside the extension.js file.

http://www.aspsnippets.com/Articles/AJAX-CalendarExtender-Disable-Past-Date-Selection.aspx

Hope this helps :)
0
 

Author Comment

by:arnololo123
ID: 38765341
I have seen this method, the one I am using is working for many other and quiet easy to implement, I just don't understand why the selected date is a day earlier.
0
 
LVL 21

Expert Comment

by:masterpass
ID: 38765406
Try something like this :

function checkDate(sender, args) {
	var selectedDate = new Date(sender._selectedDate);
	var dateToday = new Date();
	dateToday.setHours(0,0,0,0);
        if (selectedDate < dateToday) {
            alert("You cannot select a day earlier than today!");
            sender._selectedDate = new Date();
            // set the date back to the current date
            sender._textbox.set_Value(sender._selectedDate.format(sender._format))
        }
    }

Open in new window


The reason is that datepicker has time associated with it.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:arnololo123
ID: 38765606
IT still shows a day earlier which still create the alert when I select the day of today.

Regards
0
 
LVL 21

Expert Comment

by:masterpass
ID: 38765657
0
 
LVL 20

Expert Comment

by:informaniac
ID: 38766318
I feel Format="yyyy/MM/dd" could be causing the issue.

Can you change it to Format="MM/dd/yyyy" and check.
0
 

Author Comment

by:arnololo123
ID: 38767099
it does not make a difference when I change the format.
    function checkDate(sender, args) {
        var selectedDate = new Date(sender._selectedDate);
        var dateToday = new Date();
        dateToday.setHours(0, 0, 0, 0);
        selectedDate.setHours(0, 0, 0, 0);
        alert(selectedDate);alert(dateToday);
        if (selectedDate < dateToday) {

            alert("You cannot select a day earlier than today!");
            sender._selectedDate = new Date();
            // set the date back to the current date
            sender._textbox.set_Value(sender._selectedDate.format(sender._format))
        }
    }

I still have the probleme where The two alert show eaxctly one day difference, same time 00:00:00
0
 

Author Comment

by:arnololo123
ID: 38776409
Sorry to insist but  does anybody has any idea on how to fix my problem?
0
 
LVL 21

Accepted Solution

by:
masterpass earned 500 total points
ID: 38776439
modify it to something like this:

if (selectedDate < (dateToday.getTime() - 864e5)) {
            alert("You cannot select a day earlier than today!");
            sender._selectedDate = new Date();
            // set the date back to the current date
            sender._textbox.set_Value(sender._selectedDate.format(sender._format))
        }

Hope this helps.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question