How can I initiate a check on jQuery UI datepicker after the date has been selected?

I have a client side check that checks that EndDate greater than or equal to StartDate . Validation works, but it does not work as I would like. I would like it to work as soon as the date has been selected on the datepicker for EndDate . How can i do this? I tried the following:

Date Code:

 $(".datepicker").datepicker({ changeMonth: true, changeYear: true, onClose: function () { $(this).focusout(); } }); 

Security Code:

 $("#EndDate").focusout(function () { jQuery.validator.addMethod('datetimegreaterthanorequal', function (value, element, params) { var startDateValue = $(params.element).val(); return Date.parse(value) >= Date.parse(startDateValue); }, ''); jQuery.validator.unobtrusive.adapters.add('datetimegreaterthanorequal', ['startdate'], function (options) { var prefix = options.element.name.substr(0, options.element.name.lastIndexOf('.') + 1), other = options.params.startdate, fullOtherName = appendModelPrefix(other, prefix), element = $(options.form).find(':input[name=' + fullOtherName + ']')[0]; options.rules['datetimegreaterthanorequal'] = { element: element }; if (options.message) { options.messages['datetimegreaterthanorequal'] = options.message; } }); function appendModelPrefix(value, prefix) { if (value.indexOf('*.') === 0) { value = value.replace('*.', prefix); } return value; } }) 
+7
source share
2 answers

You assign validators inside our .focusout() event. Delete this block completely as you want them to be assigned only once.

You can easily activate the check for the onSelect parameter as follows:

 $(".datepicker").datepicker({ changeMonth: true, changeYear: true, onSelect: function () { $("#myForm").valid(); } }); 
+9
source

Although the answer from Mark will work, you should notice that he will check the entire form. If you just want to check the corresponding input field, you would be better off:

 $(".datepicker").datepicker({ onSelect: function () { $(this).trigger("focus").trigger("blur"); } }); 

This pushes the user by clicking inside the input field and leaving him again.

+9
source

All Articles