Skip to content

Instantly share code, notes, and snippets.

@TLiu2014
Created November 23, 2018 19:59
Show Gist options
  • Save TLiu2014/d6f94976f968af134295311520c47cb5 to your computer and use it in GitHub Desktop.
Save TLiu2014/d6f94976f968af134295311520c47cb5 to your computer and use it in GitHub Desktop.
/**
* An example of custom validator on a date string.
* Valid date is a string, which is:
* 1, in the form of YYYY-MM-DD
* 2, later than today
* 3, not an invalid value like 2018-20-81
* @param date a date string
*/
isMyDateFormat(date: string): string {
if (date.length !== 10) {
return 'Invalid input: Please input a string in the form of YYYY-MM-DD';
} else {
const da = date.split('-');
if (da.length !== 3 || da[0].length !== 4 || da[1].length !== 2 || da[2].length !== 2) {
return 'Invalid input: Please input a string in the form of YYYY-MM-DD';
} else if (moment(date).isValid()) {
return 'Invalid date: Please input a date no later than today';
} else if (!moment(date).isValid()) {
return 'Invalid date: Please input a date with a valid month and date.';
}
}
return 'Unknown error.';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment