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 longOption specifying that American Express cards are allowed.static final CodeValidatorAmerican Express (Amex) Card Validatorprivate final List<CodeValidator> The CreditCardTypes that are allowed to pass validation.static final longOption specifying that Diners cards are allowed.static final CodeValidatorDiners Card Validatorstatic final longOption specifying that Discover cards are allowed.private static final RegexValidatorDiscover Card regular expressionsstatic final CodeValidatorDiscover Card Validatorprivate static final CheckDigitLuhn checkdigit validator for the card numbers.static final longOption specifying that Mastercard cards are allowed.static final CodeValidatorMastercard Card Validatorstatic final longOption specifying that no cards are allowed.private static final longstatic final longOption specifying that Visa cards are allowed.static final CodeValidatorVisa Card Validatorstatic final longOption specifying that VPay (Visa) cards are allowed.static final CodeValidatorVPay (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 specifiedCodeValidators. -
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
-
-
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 specifiedCodeValidators.- 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
-
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.
-