Class CreditCardValidator

java.lang.Object
org.apache.commons.validator.CreditCardValidator

Deprecated.
Use the new CreditCardValidator in the routines package. This class will be removed in a future release.
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 is also available here.

Since:
1.1
  • Nested Class Summary Link icon

    Nested Classes
    Modifier and Type
    Class
    Description
    static interface 
    Deprecated.
    CreditCardType implementations define how validation is performed for one type/brand of credit card.
  • Field Summary Link icon

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Deprecated.
    Option specifying that American Express cards are allowed.
    static final int
    Deprecated.
    Option specifying that Discover cards are allowed.
    static final int
    Deprecated.
    Option specifying that Mastercard cards are allowed.
    static final int
    Deprecated.
    Option specifying that no cards are allowed.
    static final int
    Deprecated.
    Option specifying that Visa cards are allowed.
  • Constructor Summary Link icon

    Constructors
    Constructor
    Description
    Deprecated.
    Create a new CreditCardValidator with default options.
    CreditCardValidator(int options)
    Deprecated.
    Creates a new CreditCardValidator with the specified options.
  • Method Summary Link icon

    Modifier and Type
    Method
    Description
    void
    Deprecated.
    Adds an allowed CreditCardType that participates in the card validation algorithm.
    boolean
    Deprecated.
    Checks if the field is a valid credit card number.
    protected boolean
    luhnCheck(String cardNumber)
    Deprecated.
    Checks for a valid credit card number.

    Methods inherited from class java.lang.Object Link icon

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details Link icon

    • NONE Link icon

      public static final int NONE
      Deprecated.
      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);
       
       
      Since:
      1.1.2
      See Also:
    • AMEX Link icon

      public static final int AMEX
      Deprecated.
      Option specifying that American Express cards are allowed.
      See Also:
    • VISA Link icon

      public static final int VISA
      Deprecated.
      Option specifying that Visa cards are allowed.
      See Also:
    • MASTERCARD Link icon

      public static final int MASTERCARD
      Deprecated.
      Option specifying that Mastercard cards are allowed.
      See Also:
    • DISCOVER Link icon

      public static final int DISCOVER
      Deprecated.
      Option specifying that Discover cards are allowed.
      See Also:
  • Constructor Details Link icon

    • CreditCardValidator Link icon

      Deprecated.
      Create a new CreditCardValidator with default options.
    • CreditCardValidator Link icon

      public CreditCardValidator(int options)
      Deprecated.
      Creates 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.
  • Method Details Link icon

    • addAllowedCardType Link icon

      Deprecated.
      Adds an allowed CreditCardType that participates in the card validation algorithm.
      Parameters:
      type - The type that is now allowed to pass validation.
      Since:
      1.1.2
    • isValid Link icon

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

      protected boolean luhnCheck(String cardNumber)
      Deprecated.
      Checks for a valid credit card number.
      Parameters:
      cardNumber - Credit Card Number.
      Returns:
      Whether the card number passes the luhnCheck.