Class CreditCardValidator
- All Implemented Interfaces:
Serializable
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 CodeValidators
More information can be found in Michael Gilleland's essay Anatomy of Credit Card Numbers.
- Since:
- 1.4
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classClass that represents a credit card range. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final longOption specifying that American Express cards are allowed.static final CodeValidatorAmerican Express (Amex) Card Validator 34xxxx (15) 37xxxx (15)static final longOption specifying that Diners cards are allowed.static final CodeValidatorDiners Card Validator 300xxx - 305xxx (14) 3095xx (14) 36xxxx (14) 38xxxx (14) 39xxxx (14)static final longOption specifying that Discover cards are allowed.static final CodeValidatorDiscover Card Validatorstatic final longOption specifying that JCB cards are allowed.static final CodeValidatorJCB Card Validatorstatic final longOption specifying that Mastercard cards are allowed.static final CodeValidatorMastercard Card Validatorstatic final longOption specifying that no cards are allowed.static 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
ConstructorsConstructorDescriptionConstructs a new CreditCardValidator with default options.CreditCardValidator(long options) Constructs a new CreditCardValidator with the specified options.CreditCardValidator(CodeValidator[] creditCardValidators) Constructs a new CreditCardValidator with the specifiedCodeValidators.CreditCardValidator(CodeValidator[] creditCardValidators, CreditCardValidator.CreditCardRange[] creditCardRanges) Constructs a new CreditCardValidator with the specifiedCodeValidators andCreditCardValidator.CreditCardRanges.CreditCardValidator(CreditCardValidator.CreditCardRange[] creditCardRanges) Constructs a new CreditCardValidator with the specifiedCreditCardValidator.CreditCardRanges. -
Method Summary
Modifier and TypeMethodDescriptionstatic CreditCardValidatorCreates a new generic CreditCardValidator which validates the syntax and check digit only.static CreditCardValidatorgenericCreditCardValidator(int length) Creates a new generic CreditCardValidator which validates the syntax and check digit only.static CreditCardValidatorgenericCreditCardValidator(int minLen, int maxLen) Creates a new generic CreditCardValidator which validates the syntax and check digit only.booleanChecks if the field is a valid credit card number.Checks if the field is a valid credit card number.
-
Field Details
-
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:
-
JCB
public static final long JCBOption specifying that JCB cards are allowed.- Since:
- 2.10.5
- See Also:
-
AMEX_VALIDATOR
American Express (Amex) Card Validator- 34xxxx (15)
- 37xxxx (15)
-
DINERS_VALIDATOR
Diners Card Validator- 300xxx - 305xxx (14)
- 3095xx (14)
- 36xxxx (14)
- 38xxxx (14)
- 39xxxx (14)
-
DISCOVER_VALIDATOR
Discover Card Validator -
JCB_VALIDATOR
JCB Card Validator -
MASTERCARD_VALIDATOR
Mastercard Card Validator -
VISA_VALIDATOR
Visa Card Validator4xxxxx (13 or 16)
-
VPAY_VALIDATOR
VPay (Visa) Card Validator4xxxxx (13-19)
- Since:
- 1.5.0
-
-
Constructor Details
-
CreditCardValidator
public CreditCardValidator()Constructs a new CreditCardValidator with default options. The default options are: AMEX, VISA, MASTERCARD, DISCOVER and JCB -
CreditCardValidator
Constructs a new CreditCardValidator with the specifiedCodeValidators.- Parameters:
creditCardValidators- Set of valid code validators
-
CreditCardValidator
public CreditCardValidator(CodeValidator[] creditCardValidators, CreditCardValidator.CreditCardRange[] creditCardRanges) Constructs a new CreditCardValidator with the specifiedCodeValidators andCreditCardValidator.CreditCardRanges.This can be used to combine predefined validators such as
MASTERCARD_VALIDATORwith additional validators using the simplerCreditCardValidator.CreditCardRanges.- Parameters:
creditCardValidators- Set of valid code validatorscreditCardRanges- Set of valid code validators- Since:
- 1.6
-
CreditCardValidator
Constructs a new CreditCardValidator with the specifiedCreditCardValidator.CreditCardRanges.- Parameters:
creditCardRanges- Set of valid code validators- Since:
- 1.6
-
CreditCardValidator
public CreditCardValidator(long options) Constructs 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.
-
-
Method Details
-
genericCreditCardValidator
Creates 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
-
genericCreditCardValidator
Creates 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
Creates a new generic CreditCardValidator which validates the syntax and check digit only. Does not check the Issuer Identification Number (IIN)- Parameters:
minLen- minimum allowed lengthmaxLen- maximum allowed length- Returns:
- the validator
- Since:
- 1.6
-
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
nullif invalid.
-