Created
November 14, 2017 04:44
-
-
Save Ranguro/b4eb676d996b03068db06dbbd6b1e54b to your computer and use it in GitHub Desktop.
Code Sample - Date Range validator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Component date range validator used abroad date applications | |
Receives a JSON Object dateControlValue with a dateFrom and dateTo, | |
Validates that one is greater dateTo is greater than dateFrom, if that happens then validateDateLimit triggers | |
an error message. If null is returned this means the validator won't display any message. | |
Original implementation by me. | |
*/ | |
validate(control: AbstractControl): { [key: string]: any } { | |
let dateControlValue = control.value; | |
if (dateControlValue && dateControlValue.dateFrom && dateControlValue.dateTo){ | |
let dateFrom = this.parseDate(dateControlValue.dateFrom); | |
let dateTo = this.parseDate(dateControlValue.dateTo); | |
if(dateFrom > dateTo || isNaN(dateFrom.getTime()) || isNaN(dateTo.getTime()) ) return { | |
validateDateLimit: false | |
} | |
} | |
return null; | |
} | |
parseDate(controlDate: any) : Date{ | |
return typeof controlDate === 'string' ? this.dateService.string2Date(controlDate, this.dateFormat) : controlDate; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment