Class CreditCardValidator<T>

java.lang.Object
com.oorian.validation.validators.CreditCardValidator<T>
Type Parameters:
T - The type of value being validated (typically String)
All Implemented Interfaces:
Validator<T>

public class CreditCardValidator<T> extends Object implements Validator<T>
Validates credit card numbers using the Luhn algorithm.

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:
  • Constructor Details

    • CreditCardValidator

      public CreditCardValidator()
      Creates a CreditCardValidator with the default message.
    • CreditCardValidator

      public CreditCardValidator(String message)
      Creates a CreditCardValidator with a custom message.
      Parameters:
      message - The error message
  • Method Details

    • validate

      public ValidationResult validate(T value, ValidationContext context)
      Description copied from interface: Validator
      Validates the given value.

      Implementations should return ValidationResult.valid() when the value passes validation, or ValidationResult.invalid(String) with an appropriate message when it fails.

      Validators typically should not fail on null values - use RequiredValidator to enforce non-null requirements. This allows validators to be composed without unexpected failures.

      Specified by:
      validate in interface Validator<T>
      Parameters:
      value - The value to validate (may be null)
      context - The validation context for accessing other field values
      Returns:
      The validation result
    • isValidLuhn

      public static boolean isValidLuhn(String number)
      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

      public static CreditCardValidator.CardType detectCardType(String number)
      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

      public String getMessage()
      Description copied from interface: Validator
      Returns the default error message for this validator.
      Specified by:
      getMessage in interface Validator<T>
      Returns:
      The error message
    • withMessage

      public CreditCardValidator<T> withMessage(String message)
      Description copied from interface: Validator
      Creates 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:
      withMessage in interface Validator<T>
      Parameters:
      message - The custom error message
      Returns:
      A new validator instance with the custom message