start date-time and end date-time validation in angular code example
Example 1: 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')
]});
Example 2: 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')
]});