3

I am trying to setup two date pickers like this http://jqueryui.com/demos/datepicker/#date-range. But if you manually type in a date into that example you can break the code. e.g. select a from date, then the picker stops you from selecting a date before the from date, but you can manually type in a to date that is earlier.

I have set up the example here http://jsfiddle.net/Ruhley/s3h5L/

2
  • It's not obvious to me what you're asking for. Commented Dec 17, 2010 at 1:30
  • two jquery date pickers like in the demo. But the rule that the from date has to be before the to date can be broken if you manually type in a date. i can't figure out how to fix this Commented Dec 17, 2010 at 1:49

2 Answers 2

13
  1. first idea
    • use the beforeShow instead of onSelect to set the max/min date range. You still can enter manually wrong values, but as soon as you try to open a datepicker it will auto correct itself.
    • to avoid manual tampering, you can make readonly the fields.
  2. second idea
Sign up to request clarification or add additional context in comments.

Comments

1

this works ok for me but cannot change from date after click once . (by mistake) . i guess in from date picker all dates can be selectable instead of future dates

<script>    
$(function() {
    var dates = $( "#datepicker1, #datepicker2" ).datepicker({
        changeMonth: true, changeYear: true,dateFormat: "yy-mm-dd",


        beforeShow: function( ) {
            var other = this.id == "from" ? "#datepicker2" : "#datepicker1";
            var option = this.id == "from" ? "maxDate" : "minDate";
            var selectedDate = $(other).datepicker('getDate');

            $(this).datepicker( "option", option, selectedDate );
        }
    }).change(function(){
        var other = this.id == "from" ? "#datepicker2" : "#datepicker1";

        if ( $('#datepicker1').datepicker('getDate') > $('#datepicker2').datepicker('getDate') )
            $(other).datepicker('setDate', $(this).datepicker('getDate') );
    });
});
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.