isValidExpiryDate static method

bool isValidExpiryDate(
  1. String month,
  2. String year
)

Validate expiry date format and not expired.

Checks both format and expiration status:

  • Format: Month (1-12), Year (2 or 4 digits)
  • Expiration: Must not be in the past

Parameters

  • month: Expiry month (MM format)
  • year: Expiry year (YY or YYYY format)

Returns

true if the expiry date is valid and not expired, false otherwise.

Implementation

static bool isValidExpiryDate(String month, String year) {
  if (!_expiryRegex.hasMatch(month) || !_expiryRegex.hasMatch(year)) {
    return false;
  }

  final intMonth = int.parse(month);
  if (intMonth < 1 || intMonth > 12) return false;

  final intYear = int.parse(year.length == 2 ? '20$year' : year);
  final now = DateTime.now();
  final expiry = DateTime(intYear, intMonth + 1, 0); // Last day of month

  return expiry.isAfter(now);
}