org.apache.commons.validator.routines

## 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 `CodeValidator`s

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

  Since:
    Validator 1.4
  Version:
    $Revision$
  See Also:
    Serialized Form

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static class` | `CreditCardValidator.CreditCardRange` Class that represents a credit card range.

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static long` | `AMEX` Option specifying that American Express cards are allowed.
    `static CodeValidator` | `AMEX_VALIDATOR` American Express (Amex) Card Validator
    `static long` | `DINERS` Option specifying that Diners cards are allowed.
    `static CodeValidator` | `DINERS_VALIDATOR` Diners Card Validator
    `static long` | `DISCOVER` Option specifying that Discover cards are allowed.
    `static CodeValidator` | `DISCOVER_VALIDATOR` Discover Card Validator
    `static long` | `MASTERCARD` Option specifying that Mastercard cards are allowed.
    `static long` | `MASTERCARD_PRE_OCT2016` Deprecated. for use until Oct 2016 only
    `static CodeValidator` | `MASTERCARD_VALIDATOR` Mastercard Card Validator
    `static CodeValidator` | `MASTERCARD_VALIDATOR_PRE_OCT2016` Deprecated. for use until Oct 2016 only
    `static long` | `NONE` Option specifying that no cards are allowed.
    `static long` | `VISA` Option specifying that Visa cards are allowed.
    `static CodeValidator` | `VISA_VALIDATOR` Visa Card Validator
    `static long` | `VPAY` Option specifying that VPay (Visa) cards are allowed.
    `static CodeValidator` | `VPAY_VALIDATOR` VPay (Visa) Card Validator

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `CreditCardValidator()` Create a new CreditCardValidator with default options.
    `CreditCardValidator(CodeValidator[] creditCardValidators)` Create a new CreditCardValidator with the specified `CodeValidator`s.
    `CreditCardValidator(CodeValidator[] creditCardValidators, CreditCardValidator.CreditCardRange[] creditCardRanges)` Create a new CreditCardValidator with the specified `CodeValidator`s and `CreditCardValidator.CreditCardRange`s.
    `CreditCardValidator(CreditCardValidator.CreditCardRange[] creditCardRanges)` Create a new CreditCardValidator with the specified `CreditCardValidator.CreditCardRange`s.
    `CreditCardValidator(long options)` Create a new CreditCardValidator with the specified options.

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `static CreditCardValidator` | `genericCreditCardValidator()` Create a new generic CreditCardValidator which validates the syntax and check digit only.
    `static CreditCardValidator` | `genericCreditCardValidator(int length)` Create a new generic CreditCardValidator which validates the syntax and check digit only.
    `static CreditCardValidator` | `genericCreditCardValidator(int minLen, int maxLen)` Create a new generic CreditCardValidator which validates the syntax and check digit only.
    `boolean` | `isValid(String card)` Checks if the field is a valid credit card number.
    `Object` | `validate(String card)` Checks if the field is a valid credit card number.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### 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:
        Constant Field Values

    - #### AMEX

          public static final long AMEX

      Option specifying that American Express cards are allowed.
      See Also:
        Constant Field Values

    - #### VISA

          public static final long VISA

      Option specifying that Visa cards are allowed.
      See Also:
        Constant Field Values

    - #### MASTERCARD

          public static final long MASTERCARD

      Option specifying that Mastercard cards are allowed.
      See Also:
        Constant Field Values

    - #### DISCOVER

          public static final long DISCOVER

      Option specifying that Discover cards are allowed.
      See Also:
        Constant Field Values

    - #### DINERS

          public static final long DINERS

      Option specifying that Diners cards are allowed.
      See Also:
        Constant Field Values

    - #### VPAY

          public static final long VPAY

      Option specifying that VPay (Visa) cards are allowed.
      Since:
        1.5.0
      See Also:
        Constant Field Values

    - #### MASTERCARD_PRE_OCT2016

          @Deprecated
          public static final long MASTERCARD_PRE_OCT2016

      Deprecated. for use until Oct 2016 only
      Option specifying that Mastercard cards (pre Oct 2016 only) are allowed.
      See Also:
        Constant Field Values

    - #### AMEX_VALIDATOR

          public static final CodeValidator AMEX_VALIDATOR

      American Express (Amex) Card Validator

      34xxxx (15)
      37xxxx (15)

    - #### DINERS_VALIDATOR

          public static final CodeValidator DINERS_VALIDATOR

      Diners Card Validator

      300xxx - 305xxx (14)
      3095xx (14)
      36xxxx (14)
      38xxxx (14)
      39xxxx (14)

    - #### DISCOVER_VALIDATOR

          public static final CodeValidator DISCOVER_VALIDATOR

      Discover Card Validator

    - #### MASTERCARD_VALIDATOR

          public static final CodeValidator MASTERCARD_VALIDATOR

      Mastercard Card Validator

    - #### MASTERCARD_VALIDATOR_PRE_OCT2016

          @Deprecated
          public static final CodeValidator MASTERCARD_VALIDATOR_PRE_OCT2016

      Deprecated. for use until Oct 2016 only
      Mastercard Card Validator (pre Oct 2016)

    - #### VISA_VALIDATOR

          public static final CodeValidator VISA_VALIDATOR

      Visa Card Validator

      4xxxxx (13 or 16)

    - #### VPAY_VALIDATOR

          public static final CodeValidator VPAY_VALIDATOR

      VPay (Visa) Card Validator

      4xxxxx (13-19)

      Since:
        1.5.0

  - ### Constructor Detail

    - #### CreditCardValidator

          public CreditCardValidator()

      Create a new CreditCardValidator with default options. The default options are: AMEX, VISA, MASTERCARD and DISCOVER

    - #### 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 `CodeValidator`s.
      Parameters:
        `creditCardValidators` - Set of valid code validators

    - #### CreditCardValidator

          public CreditCardValidator(CreditCardValidator.CreditCardRange[] creditCardRanges)

      Create a new CreditCardValidator with the specified `CreditCardValidator.CreditCardRange`s.
      Parameters:
        `creditCardRanges` - Set of valid code validators
      Since:
        1.6

    - #### CreditCardValidator

          public CreditCardValidator(CodeValidator[] creditCardValidators,
                                     CreditCardValidator.CreditCardRange[] creditCardRanges)

      Create a new CreditCardValidator with the specified `CodeValidator`s and `CreditCardValidator.CreditCardRange`s.

      This can be used to combine predefined validators such as `MASTERCARD_VALIDATOR` with additional validators using the simpler `CreditCardValidator.CreditCardRange`s.

      Parameters:
        `creditCardValidators` - Set of valid code validators
        `creditCardRanges` - Set of valid code validators
      Since:
        1.6

  - ### Method Detail

    - #### genericCreditCardValidator

          public static CreditCardValidator genericCreditCardValidator(int minLen,
                                                                       int maxLen)

      Create 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

    - #### genericCreditCardValidator

          public static CreditCardValidator genericCreditCardValidator(int length)

      Create 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()

      Create 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

    - #### 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.

Copyright © 2002–2020 The Apache Software Foundation. All rights reserved.