3

In My date picker I have certain constraints

  1. End date should not be earlier than Start date -Done
  2. Now my problem is User should not able to select End date without selecting the Start date

My Jquery for datepicker:

var dateToday = new Date(); 
$( function() {
  $( "#datepicker1" ).datepicker({
    changeMonth: true,
    changeYear: true,
    dateFormat:'dd-M-yy',
    //   minDate: dateToday,
    onClose: function (selected) {
      $("#end").datepicker("option", "minDate", selected);
    }
  });
  $( "#end" ).datepicker({
    changeMonth: true,
    changeYear: true,
    dateFormat:'dd-M-yy',
    // minDate: dateToday,
    onClose: function (selected) {
      $("#datepicker1").datepicker("option", "maxDate", selected);
    }
  });
}); 
4
  • 2
    Possible duplicate of jQuery: enabling/disabling datepicker Commented Mar 23, 2018 at 12:17
  • Thank you so much for your reply , how to use that without affecting my previous code Commented Mar 23, 2018 at 12:18
  • 1
    Just disable the second datepicker by default and enable within onClose event handler of the first datepicker if a date value has been selected. Commented Mar 23, 2018 at 12:38
  • Ya I have disabled initially , but i could not able to enable Commented Mar 23, 2018 at 12:41

2 Answers 2

4

See the changes in:

onClose: function (selected) {
  if(selected.length <= 0) {
      // selected is empty
      $("#end").datepicker('disable')
  } else {
      $("#end").datepicker('enable');
  }
  $("#end").datepicker("option", "minDate", selected);
}

Demo here to give you an idea. Adopt to your needs.

var dateToday = new Date(); 
$( function() {
  $( "#datepicker1" ).datepicker({
    changeMonth: true,
    changeYear: true,
    dateFormat:'dd-M-yy',
    //   minDate: dateToday,
    onClose: function (selected) {
      if(selected.length <= 0) {
          // selected is empty
          $("#end").datepicker('disable');
      } else {
          $("#end").datepicker('enable');
      }
      $("#end").datepicker("option", "minDate", selected);
    }
  });
  $( "#end" ).datepicker({
    changeMonth: true,
    changeYear: true,
    dateFormat:'dd-M-yy',
    // minDate: dateToday,
    onClose: function (selected) {
      if(selected.length <= 0) {
          // selected is empty
          $("#datepicker1").datepicker('disable');
      } else {
          $("#datepicker1").datepicker('enable');
      }
      $("#datepicker1").datepicker("option", "maxDate", selected);
    }
  });
}); 
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/datepicker/0.6.4/datepicker.css">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>



<input id="datepicker1" type="text">
<input id="end" type="text">

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much its Working Perfectly
Oh, your answer is faster and better ;)
1

In first you should disable the #end datepicker by default. Then modify the onClick handler of the #datepicker1 as following:

onClose: function (selected) { 
  $("#end")
    .datepicker("option", "minDate", selected)
    .datepicker("option", "disabled", !selected);
}

2 Comments

Working perfect, If i select start date then only my end date is enabling, Once i choose both , I am deleting the start date now the end date is disabling again but its containing the latest chosen value
@Vishnu , I forget the start date may be removed. I have updated the code to lock end datepicker again when start date will be removed.

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.