I have the current template:
<input type="text" class="datepicker" name="my_date" placeholder="mm/dd/yyyy" #myDate="ngModel" [(ngModel)]="myDate" id="my_date">
<div class="error_message" *ngIf="myDate.errors?.invalidDate">
Please enter valid date
</div>
I have the following code in onInit:
setTimeout(function () {
$('.datepicker').datepicker({
autoclose: true,
dateFormat: 'MM/dd/yyyy'
}).on('hide', function(e) {
$('.datepicker').blur();
});
}, 10);
And when saving the company I have the validation logic:
if (validateDate(this.myDate) == false) {
myFormDirective.form.controls['my_date'].setErrors({'invalidDate': true});
}
The problem is that when the date is invalid datepicker put today date instead when onblur. To explain the question I'll give 2 scenarios: 1. 03/10/1800 is not valid date (less than 1900) and when onblur fired, the error is shown correctly. 2. 01/32/2002 is not valid date but, in this case datepicker instead select another valid date in year 2002 02/01/2002 instead of showing error.
Any idea how tom make sure validation happening on blur ?