Krylose Krylose - 3 months ago 35
jQuery Question

JQueryUI DatePicker replacing invalid input with minDate

I have a date field hooked up to a jQueryUI DatePicker calendar.

This DatePicker has min and max dates, though the user can still type dates outside of this range into the text field. I have server-side validation to catch these cases that returns an error message to the user and prevents them from advancing.

My problem is that when the form loads again after incorrect input, and tries to display the data already entered, the DatePicker replaces the invalid date with the closest valid date.

For example, entering a date before the min date returns the form with an error and the min date entered in the field.

Is there any way to prevent the date picker from changing this value?

Answer


Datepicker never accept any date outside on its range of min/max date.

So I have an solution for your problem
Make some changes in your datepicker as follows:

1) Remove minDate & maxDate
2) set your wrong date in datepicker field
3) set minDate/maxDate in beforeShow function

Eg:

$('#your_id').datepicker({
     dateFormat: 'dd-mm-yy',
     beforeShow: function(){
       $(this).datepicker("option", "minDate", new Date(07-02-2014) );
       $(this).datepicker("option", "maxDate", new Date(09-02-2014) );
     }
});

This could helps you a lot