I am facing a challenge with the start and enddate input tags in my HTML.
Utilizing Angular, FormBuilder, and custom Validators, I have implemented a validation system to ensure that both the start date and end date are valid.
Below is a snippet from my component showcasing the form group setup:
this.form = fb.group({
startdate: ['',Validators.compose([Validators.required,ModelValidators.validStartDate])],
enddate: ['',Validators.compose([Validators.required,ModelValidators.validEndDate])],
});
Here are the custom validators I have created:
static validStartDate(control: Control){
var valid: any;
valid=null;
var diff:any;
diff = new Date(control.value).valueOf() - new Date('1999-01-01').valueOf();
if (diff>=0){
valid=true;
}
return valid ? null : { validStartDate: true };
}
static validEndDate(control: Control){
var valid: any;
valid=null;
var diff:any;
diff = new Date().valueOf() - new Date(control.value).valueOf();
if (diff>3600*24){
valid=true;
}
return valid ? null : { validEndDate: true };
}
Now, the puzzle I need to solve is how to validate that the enddate is greater than the start date and also ensure that the difference between enddate and startdate is more than 1 year.
Is it feasible to pass additional data to the ModelValidators.validEndDate function? Any insights or suggestions on this matter would be greatly appreciated!