views:

101

answers:

3

I'm trying to enforce choosing only dates between (today minus a week, today plus a week) but instead getting a datePicker with past dates disabled and unrestricted future dates.

Any idea? (I'm new to jQuery, 2nd day of playing with it...) thanks!

I isolated the code to clearly repro it:

<html>                                                                  
<head>                                                                  
    <title>jquery sample</title>
    <script type="text/javascript" src="jquery-1.4.1.min.js"></script>
    <script type="text/javascript" src="date.js"></script>
    <script type="text/javascript" src="jquery.datePicker.js"></script>
    <link href="datePicker.css" rel="stylesheet" type="text/css" />
    <link href="custom.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" charset="utf-8">
        $(function () {
            $('.date-pick').datePicker({ minDate: '-7D', maxDate: '+7D' }).val(new Date().asString()).trigger('change');
        });
    </script>
</head>
<body>                                                                  
    <a href="">Link</a>
    <input name="date1" id="date1" class="date-pick" />
</body>                                                                 

+2  A: 
Matt Ball
I think you're writing an answer for the jQuery UI datepicker...this is a different plugin :)
Nick Craver
@Nick: Blargh. You're right. I was wondering why this wasn't working in jsfiddle...
Matt Ball
The plugin in question is: http://www.kelvinluck.com/assets/jquery/datePicker/v2/demo/
danielgwood
@Nick @Bears: I'm trying above and still not working. I'm confused by your comment Nick - am I not using jQuery UI datepicker? I meant so, I'm RTFM, http://docs.jquery.com/UI/Datepicker, thanks!
Ariel
@danielgwood: Oh, I see now. I may be pointing out to a different date picker... let me clean this up. Thanks everyone.
Ariel
A: 

I think you need to use startDate and endDate instead:

$('.date-pick').datePicker({ startDate: '-2D', endDate: '+2D' }).val(new Date().asString()).trigger('change');

That is, assuming you are using the plugin described here: kevinluck.com. This works for me on my local test. I assume you are using this plugin due to the similarity to the examples on Kevin's site.

danielgwood
+1  A: 

The options are different for your current plugin, startDate and endDate and they must be strings, like this:

$(function () {
  $('.date-pick').datePicker({ 
     startDate : new Date().addDays(-7).asString(), 
     endDate: new Date().addDays(7).asString()
  }).val(new Date().asString()).trigger('change');
});​

You can give it a try here. The documentation you seemed to have pulled from is for a different plugin, the jQuery UI Datepicker found here.

Nick Craver
@Nick, that's the case. I'm cleaning this up... Thanks!
Ariel
It doesn't look like the `startDate` and `endDate` take that format. Check out your demo again - the datepicker's looking at 1900, not 2010.
Matt Ball
@Bears: yes, noticed that. Also noticed that even after choosing a date it doesn't show up in the text control. But anyway, I appreciate to be pointed out about my confussion of using different controls, I'll figure these out later. Thanks both!
Ariel
@Ariel - @Bears make a good catch, check out the updated version, it points to the correct dates.
Nick Craver
What browser/framework gives you `addDays()`? It's definitely not standard.
Matt Ball
@Bears - It's in the `date.js` he's including, a prerequisite for `.datePicker()`, you can view it here: http://github.com/vitch/jquery-methods/raw/master/date.js
Nick Craver