isValidExpiry static method

String? isValidExpiry(
  1. String? value
)

Implementation

static String? isValidExpiry(String? value) {
  final inputValue = value ?? "";
  final regex = RegExp(r'^\d{6}$');
  if (!regex.hasMatch(inputValue)) return "Use MMYYYY.";
  int month = int.parse(inputValue.substring(0, 2));
  int year = int.parse(inputValue.substring(2, 6));
  final now = DateTime.now();

  if (month < 1 || month > 12) return "Invalid month.";
  if (year < now.year || (year == now.year && month < now.month)) {
    return "Card is expired.";
  }

  return null;
}