validateExpirationDate function
Validates the card's expiration date
The expiration date must be in one of the following date formats:
- 'MM/YY'
- 'MM/YYY'
- 'MM/YYYY'
Implementation
ValidationResults validateExpirationDate(String expDateStr) {
if (expDateStr.isEmpty) {
return ValidationResults(
isValid: false,
isPotentiallyValid: false,
message: 'No date given',
);
}
List<String> monthAndYear = _parseDate(expDateStr);
if (monthAndYear.isEmpty) {
return ValidationResults(
isValid: false,
isPotentiallyValid: false,
message: 'Invalid date format or invalid dates',
);
}
ExpMonthValidationResults monthValidation =
_validateExpMonth(monthAndYear[0]);
ExpYearValidationResults yearValidation =
_validateExpYear(monthAndYear[1]);
if (monthValidation.isValid) {
if (yearValidation.expiresThisYear) {
// If the card expires this year then tell whether or not it has expired already
return ValidationResults(
isValid: monthValidation.isValidForCurrentYear,
isPotentiallyValid: monthValidation.isValidForCurrentYear,
message: yearValidation
.message, // If year validation failed then this will be set
);
}
// Valid expiration date, all is well
if (yearValidation.isValid) {
return ValidationResults(
isValid: true,
isPotentiallyValid: true,
);
}
}
// Still a potentially valid expiration date
if (monthValidation.isPotentiallyValid &&
yearValidation.isPotentiallyValid) {
return ValidationResults(
isValid: false,
isPotentiallyValid: true,
);
}
return ValidationResults(
isValid: false,
isPotentiallyValid: false,
message: monthValidation.message,
);
}