Class CreditCardValidator
java.lang.Object
org.apache.commons.validator.routines.CreditCardValidator
- All Implemented Interfaces:
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final long
Option specifying that American Express cards are allowed.static final CodeValidator
American Express (Amex) Card Validatorprivate final List
<CodeValidator> The CreditCardTypes that are allowed to pass validation.static final long
Option specifying that Diners cards are allowed.static final CodeValidator
Diners Card Validatorstatic final long
Option specifying that Discover cards are allowed.private static final RegexValidator
Discover Card regular expressionsstatic final CodeValidator
Discover Card Validatorprivate static final CheckDigit
Luhn checkdigit validator for the card numbers.static final long
Option specifying that Mastercard cards are allowed.static final CodeValidator
Mastercard Card Validatorstatic final long
Option specifying that no cards are allowed.private static final long
static final long
Option specifying that Visa cards are allowed.static final CodeValidator
Visa Card Validatorstatic final long
Option specifying that VPay (Visa) cards are allowed.static final CodeValidator
VPay (Visa) Card Validator -
Constructor Summary
ConstructorsConstructorDescriptionCreate a new CreditCardValidator with default options.CreditCardValidator
(long options) Create a new CreditCardValidator with the specified options.CreditCardValidator
(CodeValidator[] creditCardValidators) Create a new CreditCardValidator with the specifiedCodeValidator
s. -
Method Summary
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
NONE
public static final long NONEOption 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 AMEXOption specifying that American Express cards are allowed.- See Also:
-
VISA
public static final long VISAOption specifying that Visa cards are allowed.- See Also:
-
MASTERCARD
public static final long MASTERCARDOption specifying that Mastercard cards are allowed.- See Also:
-
DISCOVER
public static final long DISCOVEROption specifying that Discover cards are allowed.- See Also:
-
DINERS
public static final long DINERSOption specifying that Diners cards are allowed.- See Also:
-
VPAY
public static final long VPAYOption specifying that VPay (Visa) cards are allowed.- Since:
- 1.5.0
- See Also:
-
cardTypes
The CreditCardTypes that are allowed to pass validation. -
LUHN_VALIDATOR
Luhn checkdigit validator for the card numbers. -
AMEX_VALIDATOR
American Express (Amex) Card Validator -
DINERS_VALIDATOR
Diners Card Validator -
DISCOVER_REGEX
Discover Card regular expressions -
DISCOVER_VALIDATOR
Discover Card Validator -
MASTERCARD_VALIDATOR
Mastercard Card Validator -
VISA_VALIDATOR
Visa Card Validator -
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
Create a new CreditCardValidator with the specifiedCodeValidator
s.- Parameters:
creditCardValidators
- Set of valid code validators
-
-
Method Details
-
isValid
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
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.
-