Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## 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, 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:
  Validator 1.4
Version:
  $Revision: 1227719 $ $Date: 2012-01-05 18:45:51 +0100 (Thu, 05 Jan 2012) $
See Also:
  Serialized Form

Field Summary
`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 CodeValidator` | `MASTERCARD_VALIDATOR` Mastercard Card Validator
`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

Constructor Summary
`CreditCardValidator()` Create a new CreditCardValidator with default options.
`CreditCardValidator(CodeValidator[] creditCardValidators)` Create a new CreditCardValidator with the specified `CodeValidator`s.
`CreditCardValidator(long options)` Create a new CreditCardValidator with the specified options.

Method Summary
`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

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

Constructor Detail

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

  Parameters:
    `creditCardValidators` - Set of valid code validators

Method Detail

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

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

Copyright © 2002-2012 The Apache Software Foundation. All Rights Reserved.