angular setvalidators start date end date custom message code example
Example: angular start date end date validation
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')
]});