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, all supported 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 implement the CreditCardType interface and pass an instance into the addAllowedCardType method.

For a similar implementation in Perl, reference Sean M. Burke's script. More information can be found in Michael Gilleland's essay Anatomy of Credit Card Numbers.

Since:
Validator 1.4
Version:
$Revision: 1713225 $
See Also:
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      See Also:
    • NONE

      public static final long NONE
      Option specifying that no cards are allowed. This is useful if you want only custom card types to validate so you turn off the default cards with this option.
       
       CreditCardValidator v = new CreditCardValidator(CreditCardValidator.NONE);
       v.addAllowedCardType(customType);
       v.isValid(aCardNumber);
       
       
      See Also:
    • AMEX

      public static final long AMEX
      Option specifying that American Express cards are allowed.
      See Also:
    • VISA

      public static final long VISA
      Option specifying that Visa cards are allowed.
      See Also:
    • MASTERCARD

      public static final long MASTERCARD
      Option specifying that Mastercard cards are allowed.
      See Also:
    • DISCOVER

      public static final long DISCOVER
      Option specifying that Discover cards are allowed.
      See Also:
    • DINERS

      public static final long DINERS
      Option specifying that Diners cards are allowed.
      See Also:
    • VPAY

      public static final long VPAY
      Option specifying that VPay (Visa) cards are allowed.
      Since:
      1.5.0
      See Also:
    • cardTypes

      private final List<CodeValidator> cardTypes
      The CreditCardTypes that are allowed to pass validation.
    • LUHN_VALIDATOR

      private static final CheckDigit LUHN_VALIDATOR
      Luhn checkdigit validator for the card numbers.
    • AMEX_VALIDATOR

      public static final CodeValidator AMEX_VALIDATOR
      American Express (Amex) Card Validator
    • DINERS_VALIDATOR

      public static final CodeValidator DINERS_VALIDATOR
      Diners Card Validator
    • DISCOVER_REGEX

      private static final RegexValidator DISCOVER_REGEX
      Discover Card regular expressions
    • DISCOVER_VALIDATOR

      public static final CodeValidator DISCOVER_VALIDATOR
      Discover Card Validator
    • MASTERCARD_VALIDATOR

      public static final CodeValidator MASTERCARD_VALIDATOR
      Mastercard Card Validator
    • VISA_VALIDATOR

      public static final CodeValidator VISA_VALIDATOR
      Visa Card Validator
    • VPAY_VALIDATOR

      public static final CodeValidator VPAY_VALIDATOR
      VPay (Visa) Card Validator
      Since:
      1.5.0
  • Constructor Details

    • CreditCardValidator

      public CreditCardValidator()
      Create a new CreditCardValidator with default options.
    • CreditCardValidator

      public CreditCardValidator(long options)
      Create a new CreditCardValidator with the specified options.
      Parameters:
      options - Pass in CreditCardValidator.VISA + CreditCardValidator.AMEX to specify that those are the only valid card types.
    • CreditCardValidator

      public CreditCardValidator(CodeValidator[] creditCardValidators)
      Create a new CreditCardValidator with the specified CodeValidators.
      Parameters:
      creditCardValidators - Set of valid code validators
  • Method Details

    • 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.
    • isOn

      private boolean isOn(long options, long flag)
      Tests whether the given flag is on. If the flag is not a power of 2 (ie. 3) this tests whether the combination of flags is on.
      Parameters:
      options - The options specified.
      flag - Flag value to check.
      Returns:
      whether the specified flag value is on.