user3150977
user3150977

Reputation: 33

bootstrap datepicker enddate can't be more than 1 year after startdate

I have 2 input boxes with datepicker. I want following validations

  1. startdate can't be in the future
  2. enddate can't be before startdate
  3. enddate can't be more than 1 year after startdate
$('#startdate').datepicker({
        format: 'dd-mm-yyyy',
        autoclose: true,
        weekStart: 1,
        endDate: "today"
    });

    $('#enddate').datepicker({
        language: 'nl',
        format: 'dd-mm-yyyy',
        autoclose: true,
        weekStart: 1,
        endDate: "today"
    });

First validation is working fine. Please help me with other 2 validations. Thanks in advance

Upvotes: 2

Views: 106

Answers (1)

Yogesh Patel
Yogesh Patel

Reputation: 722

You can try this

Since your 1st condition is working I have included condition 2 and 3 in changeDate function

$('#startdate').datepicker({
    format: 'dd-mm-yyyy',
    autoclose: true,
    weekStart: 1,
    endDate: "today"
}).on('changeDate', function(){
    // set the "enddate" start to not be later than "startdate"
    $('#enddate').datepicker('setStartDate', new Date($(this).val()));
    $('#enddate').datepicker('setEndDate', '+1y');
});

$('#enddate').datepicker({
    language: 'nl',
    format: 'dd-mm-yyyy',
    autoclose: true,
    weekStart: 1,
    endDate: "today"
});

Upvotes: 2

Related Questions