cardExpiryDateValidator function
Implementation
String? cardExpiryDateValidator(String? value) {
if (value == null || value.isEmpty) {
return 'Expiry Date is Required';
}
final regex = RegExp(r'^\d{2}/\d{2}$');
if (!regex.hasMatch(value)) {
return 'Invalid format (MM/YY)';
}
final parts = value.split('/');
final month = int.tryParse(parts[0]) ?? 0;
final year = int.tryParse(parts[1]) ?? 0;
if (month < 1 || month > 12) {
return 'Invalid month';
}
final currentYear = DateTime.now().year;
final currentMonth = DateTime.now().month;
final cardYear = 2000 + year;
if (cardYear < currentYear ||
(cardYear == currentYear && month < currentMonth)) {
return 'Card has expired';
}
return null;
}