from date and to date validation in angular 8 code example
Example: from date and to date validation in angular 8
export class CustomeDateValidators {
static fromToDate(fromDateField: string, toDateField: string, errorName: string = 'fromToDate'): ValidatorFn {
return (formGroup: AbstractControl): { [key: string]: boolean } | null => {
const fromDate = formGroup.get(fromDateField).value;
const toDate = formGroup.get(toDateField).value;
if ((fromDate !== null && toDate !== null) && fromDate > toDate) {
return {[errorName]: true};
}
return null;
};
}
}
this.form = this.fb.group({
fromDate: null,
toDate: null,
}, { validator: [
CustomeDateValidators.fromToDate('fromDate', 'toDate')
]});