Package org.apache.commons.validator
Class CreditCardValidator
- java.lang.Object
-
- org.apache.commons.validator.CreditCardValidator
-
public class CreditCardValidator extends java.lang.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,
For a similar implementation in Perl, reference Sean M. Burke's script. More information is also available here.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 theaddAllowedCardType
method.- Since:
- Validator 1.1
- Version:
- $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
CreditCardValidator.Amex
static interface
CreditCardValidator.CreditCardType
CreditCardType implementations define how validation is performed for one type/brand of credit card.private class
CreditCardValidator.Discover
private class
CreditCardValidator.Mastercard
private class
CreditCardValidator.Visa
Change to support Visa Carte Blue used in France has been removed - see Bug 35926
-
Field Summary
Fields Modifier and Type Field Description static int
AMEX
Option specifying that American Express cards are allowed.private java.util.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
Constructors Constructor Description CreditCardValidator()
Create a new CreditCardValidator with default options.CreditCardValidator(int options)
Create a new CreditCardValidator with the specified options.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addAllowedCardType(CreditCardValidator.CreditCardType type)
Add an allowed CreditCardType that participates in the card validation algorithm.boolean
isValid(java.lang.String card)
Checks if the field is a valid credit card number.protected boolean
luhnCheck(java.lang.String cardNumber)
Checks for a valid credit card number.
-
-
-
Field Detail
-
NONE
public static final int 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);
- Since:
- Validator 1.1.2
- See Also:
- Constant Field Values
-
AMEX
public static final int AMEX
Option specifying that American Express cards are allowed.- See Also:
- Constant Field Values
-
VISA
public static final int VISA
Option specifying that Visa cards are allowed.- See Also:
- Constant Field Values
-
MASTERCARD
public static final int MASTERCARD
Option specifying that Mastercard cards are allowed.- See Also:
- Constant Field Values
-
DISCOVER
public static final int DISCOVER
Option specifying that Discover cards are allowed.- See Also:
- Constant Field Values
-
cardTypes
private java.util.Collection cardTypes
The CreditCardTypes that are allowed to pass validation.
-
-
Constructor Detail
-
CreditCardValidator
public CreditCardValidator()
Create a new CreditCardValidator with default options.
-
CreditCardValidator
public CreditCardValidator(int 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.
-
-
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.
-
addAllowedCardType
public void addAllowedCardType(CreditCardValidator.CreditCardType type)
Add an allowed CreditCardType that participates in the card validation algorithm.- Parameters:
type
- The type that is now allowed to pass validation.- Since:
- Validator 1.1.2
-
luhnCheck
protected boolean luhnCheck(java.lang.String cardNumber)
Checks for a valid credit card number.- Parameters:
cardNumber
- Credit Card Number.- Returns:
- Whether the card number passes the luhnCheck.
-
-