Class CreditCardValidator
- java.lang.Object
-
- org.apache.commons.validator.routines.CreditCardValidator
-
- All Implemented Interfaces:
java.io.Serializable
public class CreditCardValidator extends java.lang.Object implements java.io.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:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static long
AMEX
Option specifying that American Express cards are allowed.static CodeValidator
AMEX_VALIDATOR
American Express (Amex) Card Validatorprivate 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 Validatorstatic long
DISCOVER
Option specifying that Discover cards are allowed.private static RegexValidator
DISCOVER_REGEX
Discover Card regular expressionsstatic CodeValidator
DISCOVER_VALIDATOR
Discover Card Validatorprivate 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 Validatorstatic 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 Validatorstatic long
VPAY
Option specifying that VPay (Visa) cards are allowed.static CodeValidator
VPAY_VALIDATOR
VPay (Visa) Card Validator
-
Constructor Summary
Constructors Constructor Description CreditCardValidator()
Create 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
All Methods Instance Methods Concrete Methods Modifier and Type Method 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.
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
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
-
VPAY
public static final long VPAY
Option specifying that VPay (Visa) cards are allowed.- Since:
- 1.5.0
- See Also:
- Constant Field Values
-
cardTypes
private final java.util.List<CodeValidator> cardTypes
The CreditCardTypes that are allowed to pass validation.
-
LUHN_VALIDATOR
private static final CheckDigit LUHN_VALIDATOR
Luhn checkdigit validator for the card numbers.
-
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_REGEX
private static final RegexValidator DISCOVER_REGEX
Discover Card regular expressions
-
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
-
VPAY_VALIDATOR
public static final CodeValidator VPAY_VALIDATOR
VPay (Visa) Card Validator- Since:
- 1.5.0
-
-
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 specifiedCodeValidator
s.- Parameters:
creditCardValidators
- Set of valid code validators
-
-
Method Detail
-
isValid
public boolean isValid(java.lang.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 java.lang.Object validate(java.lang.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.
-
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.
-
-