I'm trying to validate the order_datePicker input field when it is filled by bootstrap-datepicker. I've already found some answers on stackoverflow but somehow I cant get it to work myself. With the other input fields i'm using:
$('#order_emailadres').on('input', function() {
However this isnt working for a datepicker input field because the user is not using input.
So, as mentioned in the documentation and stackoverflow link i'm trying to catch the changeDate event so that the input can be validated by the following code:
HTML:
<div id=sandbox-container>
<div id="datepicker"></div>
<input type="text" id="order_datePicker" name=order_datePicker>
</div>
</div>
JS:
$('#order_datePicker').on('changeDate', function () {
var input=$(this);
var re = /^[0-9]{2}-[0-9]{2}-[0-9]{4}$/;
var is_valid=re.test(input.val());
if(is_valid){input.removeClass("invalid").addClass("valid");}
else{input.removeClass("valid").addClass("invalid");}
});
Sadly this is not working. What am I doing wrong?
Update:
Got it working with the code below but now the class is added to the div instead of the input field. However the original question is answered. Thanks
$('#datepicker').datepicker().on('changeDate',function(){
$('#order_datePicker').val($('#datepicker').datepicker('getFormattedDate'));
var input=$(this);
var re = /^[0-9]{2}-[0-9]{2}-[0-9]{4}$/;
var is_valid=re.test(input.val());
if(is_valid){input.removeClass("invalid").addClass("valid");}
else{input.removeClass("valid").addClass("invalid");}
});