Search code examples
javascriptjqueryhtmljquery-ui-datepickerbootstrap-datepicker

Constraint endDate after select startDate


I need help to add a constraint that endDate (date_end) will always be after the startDate (date_start).

So if I had selected 2016-10-31 for the date_start, then the dates before 2016-10-31 will be disabled in the end_date input.

Here is my code:

<script src="http://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/js/bootstrap-datepicker.js"></script>

<section class="content">
  <div class="box box-primary">
    <div class="box-body">
      <form class="form-horizontal mt-20" method="post" action="action/input">
        <div class="form-group">
          <label class="col-sm-3 control-label">Name</label>
          <div class="col-sm-5">
            <select name="date" class="form-control select2" id="date" data-placeholder="" style="width: 100%;">
              <option disabled selected>Select name</option>
              <option value='2' data-date_start="2016-10-04" data-date_end="2016-10-14">Anton</option>
              <option value='4' data-date_start="2016-10-09" data-date_end="2016-10-29">Boby</option>
              <option value='5' data-date_start="2016-10-01" data-date_end="2016-10-31">Ciara</option>
              <option value='6' data-date_start="2016-10-05" data-date_end="2016-10-22">Don</option>
              <option value='7' data-date_start="2016-10-01" data-date_end="2016-10-17">Ester</option>
            </select>
          </div>
        </div>
        <div class="form-group">
          <label class="col-sm-3 control-label">Start Date</label>
          <div class="col-sm-5">
            <div class="input-group date">
              <div class="input-group-addon">
                <i class="fa fa-calendar"></i>
              </div>
              <input id="start_date" name="start_date" type="text" class="form-control pull-right">
            </div>
          </div>
        </div>
        <div class="form-group">
          <label class="col-sm-3 control-label">End Date</label>
          <div class="col-sm-5">
            <div class="input-group date">
              <div class="input-group-addon">
                <i class="fa fa-calendar"></i>
              </div>
              <input id="end_date" name="end_date" type="text" class="form-control pull-right">
            </div>
          </div>
        </div>
    </div>

    <div class="box-footer col-md-offset-3">
      <button type="submit" class="btn btn-success mr-10">Save</button>
      <button type="button" class="btn btn-primary" onclick="history.go(-1)">Cancel</button>
    </div>

    </form>
  </div>
</section>

js:

$(document).ready(function() {
  var start = null;
  var end = null;
  $('#date').change(function() {

    start = $(this).find(":selected").data("date_start");
    end = $(this).find(":selected").data("date_end");
    $('.input-group.date').datepicker("remove"); //clearing the previos options and again initiating datepicker
    initDatePicker();
  });

  function initDatePicker() {
    $('.input-group.date').datepicker({
      format: "yyyy-mm-dd",
      startDate: new Date(start),
      endDate: new Date(end),
      autoclose: false
    });
  }

});

Solution

  • You can add an event handler for onchange to the startDate and endDate pickers, with the setter method. See example below.

    Additions/modifications:

    1. Modify the date-group divs to have an id attribute:

      <div class="input-group date" id="start_date_group">

      2.Register the changeDate callback function for each date picker - e.g.:

      $("#start_date_group").on("changeDate", function(e) { $('#end_date_group').datepicker('setStartDate',e.date); }); $("#end_date_group").on("changeDate", function(e) { $('#start_date_group').datepicker('setEndDate',e.date); });

      1. There is also a </div> tag that is out of place in your html - I moved it from after the endDate group to after the closing tag of the form.

    $(document).ready(function() {
      var start = null;
      var end = null;
      $('#date').change(function() {
    
        start = $(this).find(":selected").data("date_start");
        end = $(this).find(":selected").data("date_end");
        $('.input-group.date').datepicker("remove"); //clearing the previos options and again initiating datepicker
        initDatePicker();
      });
    
      function initDatePicker() {
        $('.input-group.date').datepicker({
          format: "yyyy-mm-dd",
          startDate: new Date(start),
          endDate: new Date(end),
          autoclose: false
        });
        //when the Start date changes, set the startDate on the End Date field
        $("#start_date_group").on("changeDate", function(e) {
          $('#end_date_group').datepicker('setStartDate',e.date);
        });
        //when the End date changes, set the endDate on the Start Date field
        $("#end_date_group").on("changeDate", function(e) {
          $('#start_date_group').datepicker('setEndDate',e.date);
        });
      }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/js/bootstrap-datepicker.js"></script>
    <link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/bootstrap/latest/css/bootstrap.css" />
    <link rel="stylesheet" type="text/css" href="https://uxsolutions.github.io/bootstrap-datepicker/bootstrap-datepicker/css/bootstrap-datepicker3.min.css" />
    <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" />
    <section class="content">
      <div class="box box-primary">
        <div class="box-body">
          <form class="form-horizontal mt-20" method="post" action="action/input">
            <div class="form-group">
              <label class="col-sm-3 control-label">Name</label>
              <div class="col-sm-5">
                <select name="date" class="form-control select2" id="date" data-placeholder="" style="width: 100%;">
                  <option disabled selected>Select name</option>
                  <option value='2' data-date_start="2016-10-04" data-date_end="2016-10-14">Anton</option>
                  <option value='4' data-date_start="2016-10-09" data-date_end="2016-10-29">Boby</option>
                  <option value='5' data-date_start="2016-10-01" data-date_end="2016-10-31">Ciara</option>
                  <option value='6' data-date_start="2016-10-05" data-date_end="2016-10-22">Don</option>
                  <option value='7' data-date_start="2016-10-01" data-date_end="2016-10-17">Ester</option>
                </select>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-3 control-label">Start Date</label>
              <div class="col-sm-5">
                <!-- add id attribute to the group here -->
                <div class="input-group date" id="start_date_group">
                  <div class="input-group-addon">
                    <i class="fa fa-calendar"></i>
                  </div>
                  <input id="start_date" name="start_date" type="text" class="form-control pull-right">
                </div>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-3 control-label">End Date</label>
              <div class="col-sm-5">                
                <!-- add id attribute to the group here -->
                <div class="input-group date" id="end_date_group">
                  <div class="input-group-addon">
                    <i class="fa fa-calendar"></i>
                  </div>
                  <input id="end_date" name="end_date" type="text" class="form-control pull-right">
                </div>
              </div>
            </div>
         <!--</div> move this closing tag down after the closing form tag -->
            <div class="box-footer col-md-offset-3">
              <button type="submit" class="btn btn-success mr-10">Save</button>
              <button type="button" class="btn btn-primary" onclick="history.go(-1)">Cancel</button>
            </div>
    
          </form>
        </div>
      </div> <!-- <-- move this closing div tag down here instead of after the endDategroup <div>-->
    </section>