org.apache.commons.validator
public class CreditCardValidator extends Object
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.
Since: Validator 1.1
Nested Class Summary | |
---|---|
class | CreditCardValidator.Amex |
interface | CreditCardValidator.CreditCardType
CreditCardType implementations define how validation is performed
for one type/brand of credit card. |
class | CreditCardValidator.Discover |
class | CreditCardValidator.Mastercard |
class | CreditCardValidator.Visa |
Field Summary | |
---|---|
static int | AMEX
Option specifying that American Express cards are allowed. |
Collection | cardTypes
The CreditCardTypes that are allowed to pass validation. |
static int | DISCOVER
Option specifying that Discover cards are allowed. |
static int | MASTERCARD
Option specifying that Mastercard cards are allowed. |
static int | NONE
Option specifying that no cards are allowed. |
static int | VISA
Option specifying that Visa cards are allowed. |
Constructor Summary | |
---|---|
CreditCardValidator()
Create a new CreditCardValidator with default options. | |
CreditCardValidator(int options)
Create a new CreditCardValidator with the specified options. |
Method Summary | |
---|---|
void | addAllowedCardType(CreditCardValidator.CreditCardType type)
Add an allowed CreditCardType that participates in the card
validation algorithm. |
boolean | isValid(String card)
Checks if the field is a valid credit card number. |
protected boolean | isValidPrefix(String card)
Checks for a valid credit card number. |
protected boolean | luhnCheck(String cardNumber)
Checks for a valid credit card number. |
CreditCardValidator v = new CreditCardValidator(CreditCardValidator.NONE); v.addAllowedCardType(customType); v.isValid(aCardNumber);
Since: Validator 1.1.2
Parameters: options Pass in CreditCardValidator.VISA + CreditCardValidator.AMEX to specify that those are the only valid card types.
Parameters: type The type that is now allowed to pass validation.
Since: Validator 1.1.2
Parameters: card The card number to validate.
Deprecated: This will be removed in a future release.
Checks for a valid credit card number.Parameters: card Credit Card Number.
Parameters: cardNumber Credit Card Number.