public class CreditCardValidator
extends java.lang.Object
implements java.io.Serializable
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.
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
|
private java.util.List<CodeValidator> |
cardTypes
The CreditCardTypes that are allowed to pass validation.
|
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.
|
private static RegexValidator |
DISCOVER_REGEX
Discover Card regular expressions
|
static CodeValidator |
DISCOVER_VALIDATOR
Discover Card Validator
|
private static CheckDigit |
LUHN_VALIDATOR
Luhn checkdigit validator for the card numbers.
|
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.
|
private static long |
serialVersionUID |
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 and Description |
---|
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.
|
Modifier and Type | Method and Description |
---|---|
private boolean |
isOn(long options,
long flag)
Tests whether the given flag is on.
|
boolean |
isValid(java.lang.String card)
Checks if the field is a valid credit card number.
|
java.lang.Object |
validate(java.lang.String card)
Checks if the field is a valid credit card number.
|
private static final long serialVersionUID
public static final long NONE
CreditCardValidator v = new CreditCardValidator(CreditCardValidator.NONE);
v.addAllowedCardType(customType);
v.isValid(aCardNumber);
public static final long AMEX
public static final long VISA
public static final long MASTERCARD
public static final long DISCOVER
public static final long DINERS
public static final long VPAY
private final java.util.List<CodeValidator> cardTypes
private static final CheckDigit LUHN_VALIDATOR
public static final CodeValidator AMEX_VALIDATOR
public static final CodeValidator DINERS_VALIDATOR
private static final RegexValidator DISCOVER_REGEX
public static final CodeValidator DISCOVER_VALIDATOR
public static final CodeValidator MASTERCARD_VALIDATOR
public static final CodeValidator VISA_VALIDATOR
public static final CodeValidator VPAY_VALIDATOR
public CreditCardValidator()
public CreditCardValidator(long options)
options
- Pass in
CreditCardValidator.VISA + CreditCardValidator.AMEX to specify that
those are the only valid card types.public CreditCardValidator(CodeValidator[] creditCardValidators)
CodeValidator
s.creditCardValidators
- Set of valid code validatorspublic boolean isValid(java.lang.String card)
card
- The card number to validate.public java.lang.Object validate(java.lang.String card)
card
- The card number to validate.null
if invalid.private boolean isOn(long options, long flag)
options
- The options specified.flag
- Flag value to check.