Class CreditCardValidator<T>
- Type Parameters:
T- The type of value being validated (typically String)
- All Implemented Interfaces:
Validator<T>
CreditCardValidator checks that a value is a valid credit card number. It uses the Luhn algorithm (also known as the mod 10 algorithm) to validate the number and can optionally detect the card type.
Null and empty values pass validation - use RequiredValidator to enforce non-null.
Supported Card Types:
- Visa - Starts with 4, 13-16 digits
- Mastercard - Starts with 51-55 or 2221-2720, 16 digits
- American Express - Starts with 34 or 37, 15 digits
- Discover - Starts with 6011, 622126-622925, 644-649, or 65, 16 digits
- Diners Club - Starts with 300-305, 36, 38, 14-16 digits
- JCB - Starts with 3528-3589, 16 digits
Usage:
// Basic validation
CreditCardValidator<String> validator = new CreditCardValidator<>();
validator.validate("4111111111111111", context); // Valid (Visa)
validator.validate("1234567890123456", context); // Invalid (fails Luhn)
// Detect card type
CardType type = CreditCardValidator.detectCardType("4111111111111111");
// Returns CardType.VISA
// With custom message
CreditCardValidator<String> custom = new CreditCardValidator<String>()
.withMessage("Please enter a valid credit card number");
// Fluent in ValidatedInput
new ValidatedInput<>(input, errorSpan)
.required()
.creditCard();
- Since:
- 2025
- Version:
- 1.0
- Author:
- Marvin P. Warble Jr.
- See Also:
-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsConstructorDescriptionCreates a CreditCardValidator with the default message.CreditCardValidator(String message) Creates a CreditCardValidator with a custom message. -
Method Summary
Modifier and TypeMethodDescriptionstatic CreditCardValidator.CardTypedetectCardType(String number) Detects the card type based on the card number prefix.Returns the default error message for this validator.static booleanisValidLuhn(String number) Validates a credit card number using the Luhn algorithm.validate(T value, ValidationContext context) Validates the given value.withMessage(String message) Creates a new validator with a custom error message.
-
Constructor Details
-
CreditCardValidator
public CreditCardValidator()Creates a CreditCardValidator with the default message. -
CreditCardValidator
Creates a CreditCardValidator with a custom message.- Parameters:
message- The error message
-
-
Method Details
-
validate
Description copied from interface:ValidatorValidates the given value.Implementations should return
ValidationResult.valid()when the value passes validation, orValidationResult.invalid(String)with an appropriate message when it fails.Validators typically should not fail on null values - use
RequiredValidatorto enforce non-null requirements. This allows validators to be composed without unexpected failures. -
isValidLuhn
Validates a credit card number using the Luhn algorithm.The Luhn algorithm (also known as the "modulus 10" algorithm) is a simple checksum formula used to validate various identification numbers.
- Parameters:
number- The credit card number (digits only)- Returns:
- true if the number passes the Luhn check
-
detectCardType
Detects the card type based on the card number prefix.- Parameters:
number- The credit card number- Returns:
- The detected card type, or UNKNOWN if not recognized
-
getMessage
Description copied from interface:ValidatorReturns the default error message for this validator.- Specified by:
getMessagein interfaceValidator<T>- Returns:
- The error message
-
withMessage
Description copied from interface:ValidatorCreates a new validator with a custom error message.This method returns a new instance with the custom message, leaving the original validator unchanged.
- Specified by:
withMessagein interfaceValidator<T>- Parameters:
message- The custom error message- Returns:
- A new validator instance with the custom message
-