isCreditExpiryOnOrAfterMonth function

bool isCreditExpiryOnOrAfterMonth(
  1. String value, {
  2. DateTime? at,
})

True when value is valid MM/YY and the expiry month is still current or in the future relative to at (defaults to DateTime.now). Cards are treated as valid through the last day of the printed month.

Two-digit years are interpreted as 20YY (common for payment UIs).

Implementation

bool isCreditExpiryOnOrAfterMonth(String value, {DateTime? at}) {
  if (!CreditExpirySubmitRegexValidator().isValid(value)) {
    return false;
  }
  final parts = value.split('/');
  final month = int.parse(parts[0], radix: 10);
  final yy = int.parse(parts[1], radix: 10);
  final fullYear = 2000 + yy;
  final now = at ?? DateTime.now();
  if (fullYear > now.year) {
    return true;
  }
  if (fullYear < now.year) {
    return false;
  }
  return month >= now.month;
}