In this article we will learn about some of the frequently asked TypeScript programming questions in technical like “from date and to date validation in angular 9” Code Answer’s. When creating scripts and web applications, error handling is an important part. If your code lacks error checking code, your program may look very unprofessional and you may be open to security risks. Error or stack handling on typescript was simple and easy. An error message with filename, line number and a message describing the error is sent to the browser. This tutorial contains some of the most common error checking methods in Typescript. Below are some solution about “from date and to date validation in angular 9” Code Answer’s.
angular start date end date validation
xxxxxxxxxx
1
export 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 ---*/
16
this.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
from date and to date validation in angular 9
xxxxxxxxxx
1
export 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 ---*/
16
this.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