validateCreditCard static method
Validate credit card number
Implementation
static String? validateCreditCard(String? value) {
if (value == null || value.isEmpty) {
return 'Credit card number is required.';
}
// Remove spaces and dashes
String cleanNumber = value.replaceAll(RegExp(r'[\s-]'), '');
// Check if it contains only digits
if (!RegExp(r'^\d+$').hasMatch(cleanNumber)) {
return 'Credit card number should contain only digits.';
}
// Check length (typically 13-19 digits)
if (cleanNumber.length < 13 || cleanNumber.length > 19) {
return 'Invalid credit card number length.';
}
// Luhn algorithm validation
if (!_luhnCheck(cleanNumber)) {
return 'Invalid credit card number.';
}
return null;
}