angular start date end date validation

Solutions on MaxInterview for angular start date end date validation by the best coders in the world

showing results for - "angular start date end date validation"
Noah
15 Nov 2020
1export class CustomeDateValidators {
2    static fromToDate(fromDateField: string, toDateField: string, errorName: string = 'fromToDate'): ValidatorFn {
3        return (formGroup: AbstractControl): { [key: string]: boolean } | null => {
4            const fromDate = formGroup.get(fromDateField).value;
5            const toDate = formGroup.get(toDateField).value;
6           // Ausing the fromDate and toDate are numbers. In not convert them first after null check
7            if ((fromDate !== null && toDate !== null) && fromDate > toDate) {
8                return {[errorName]: true};
9            }
10            return null;
11        };
12    }
13}
14
15/*--- implementations ---*/
16this.form = this.fb.group({
17  fromDate: null,
18  toDate: null,
19}, { validator: [
20  //Default error with this validator:  {fromToDate: true}
21  CustomeDateValidators.fromToDate('fromDate', 'toDate')
22  
23  // For custome error name like: {customeErrorName: true}, pass third optional parameter with custome name
24  // CustomeDateValidators.fromToDate('fromDate', 'toDate', 'customeErrorName')
25]});
26
27