Class CreditCardValidator

java.lang.Object
org.apache.commons.validator.routines.CreditCardValidator
All Implemented Interfaces:
Serializable

public class CreditCardValidator extends Object implements Serializable
Perform credit card validations.

By default, AMEX + VISA + MASTERCARD + DISCOVER card types are allowed. You can specify which cards should pass validation by configuring the validation options. For example,

 CreditCardValidator ccv = new CreditCardValidator(CreditCardValidator.AMEX + CreditCardValidator.VISA);
 

configures the validator to only pass American Express and Visa cards. If a card type is not directly supported by this class, you can create an instance of the CodeValidator class and pass it to a CreditCardValidator constructor along with any existing validators. For example:

 CreditCardValidator ccv = new CreditCardValidator(
     new CodeValidator[] {
         CreditCardValidator.AMEX_VALIDATOR,
         CreditCardValidator.VISA_VALIDATOR,
         new CodeValidator("^(4)(\\d{12,18})$", LUHN_VALIDATOR) // add VPAY
 };
 

Alternatively you can define a validator using the CreditCardValidator.CreditCardRange class. For example:

 CreditCardValidator ccv = new CreditCardValidator(
    new CreditCardRange[]{
        new CreditCardRange("300", "305", 14, 14), // Diners
        new CreditCardRange("3095", null, 14, 14), // Diners
        new CreditCardRange("36",   null, 14, 14), // Diners
        new CreditCardRange("38",   "39", 14, 14), // Diners
        new CreditCardRange("4",    null, new int[] {13, 16}), // VISA
    }
 );
 
 

This can be combined with a list of CodeValidators

More information can be found in Michael Gilleland's essay Anatomy of Credit Card Numbers.

Since:
1.4
See Also:
  • Field Details

  • Constructor Details

  • Method Details

    • genericCreditCardValidator

      Creates a new generic CreditCardValidator which validates the syntax and check digit only. Does not check the Issuer Identification Number (IIN)
      Returns:
      the validator
      Since:
      1.6
    • genericCreditCardValidator

      public static CreditCardValidator genericCreditCardValidator(int length)
      Creates a new generic CreditCardValidator which validates the syntax and check digit only. Does not check the Issuer Identification Number (IIN)
      Parameters:
      length - exact length
      Returns:
      the validator
      Since:
      1.6
    • genericCreditCardValidator

      public static CreditCardValidator genericCreditCardValidator(int minLen, int maxLen)
      Creates a new generic CreditCardValidator which validates the syntax and check digit only. Does not check the Issuer Identification Number (IIN)
      Parameters:
      minLen - minimum allowed length
      maxLen - maximum allowed length
      Returns:
      the validator
      Since:
      1.6
    • isValid

      public boolean isValid(String card)
      Checks if the field is a valid credit card number.
      Parameters:
      card - The card number to validate.
      Returns:
      Whether the card number is valid.
    • validate

      public Object validate(String card)
      Checks if the field is a valid credit card number.
      Parameters:
      card - The card number to validate.
      Returns:
      The card number if valid or null if invalid.