Class CreditCardValidator
- java.lang.Object
-
- org.apache.wicket.validation.validator.CreditCardValidator
-
- All Implemented Interfaces:
java.io.Serializable,org.apache.wicket.util.io.IClusterable,IValidator<java.lang.String>
public class CreditCardValidator extends java.lang.Object implements IValidator<java.lang.String>
Checks if a credit card number is valid. The number will be checked for "American Express", "China UnionPay", "Diners Club Carte Blanche", "Diners Club International", "Diners Club US & Canada", "Discover Card", "JCB", "Laser", "Maestro", "MasterCard", "Solo", "Switch", "Visa" and "Visa Electron". If none of those apply to the credit card number, the credit card number is considered invalid.Card prefixes and lengths have been taken from Wikipedia.
- Since:
- 1.2.6
- Author:
- Johan Compagner, Joachim F. Rohde
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCreditCardValidator.CreditCard
-
Constructor Summary
Constructors Constructor Description CreditCardValidator()Construct.CreditCardValidator(boolean failOnUnknown)Construct.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected IValidationErrordecorate(IValidationError error, IValidatable<java.lang.String> validatable)Allows subclasses to decorate reported errorsCreditCardValidator.CreditCarddetermineCardId(java.lang.String creditCardNumber)Checks if the credit card number can be determined as a valid number.CreditCardValidator.CreditCardgetCardId()protected booleanisChecksumCorrect(java.lang.String creditCardNumber)Calculates the checksum of a credit card number using the Luhn algorithm (the so-called "mod 10" algorithm).protected booleanisLengthAndPrefixCorrect(java.lang.String creditCardNumber)Checks if the credit card number can be determined as a valid number.protected CreditCardValidator.CreditCardisUnknown(java.lang.String creditCardNumber)Can be used (subclassed) to extend the test with a credit card not yet known by the validator.protected voidsetCardId(CreditCardValidator.CreditCard cardId)Allow subclasses to set the card idvoidvalidate(IValidatable<java.lang.String> validatable)Validates theIValidatableinstance.
-
-
-
Method Detail
-
getCardId
public final CreditCardValidator.CreditCard getCardId()
- Returns:
- Credit card issuer
-
setCardId
protected void setCardId(CreditCardValidator.CreditCard cardId)
Allow subclasses to set the card id- Parameters:
cardId-
-
validate
public void validate(IValidatable<java.lang.String> validatable)
Description copied from interface:IValidatorValidates theIValidatableinstance. Validation errors should be reported using theIValidatable.error(IValidationError)method.- Specified by:
validatein interfaceIValidator<java.lang.String>- Parameters:
validatable- theIValidatableinstance being validated
-
decorate
protected IValidationError decorate(IValidationError error, IValidatable<java.lang.String> validatable)
Allows subclasses to decorate reported errors- Parameters:
error-validatable-- Returns:
- decorated error
-
isLengthAndPrefixCorrect
protected boolean isLengthAndPrefixCorrect(java.lang.String creditCardNumber)
Checks if the credit card number can be determined as a valid number.- Parameters:
creditCardNumber- the credit card number as a string- Returns:
TRUEif the credit card number could be determined as a valid number, elseFALSEis returned
-
determineCardId
public final CreditCardValidator.CreditCard determineCardId(java.lang.String creditCardNumber)
Checks if the credit card number can be determined as a valid number.- Parameters:
creditCardNumber- the credit card number as a string- Returns:
TRUEif the credit card number could be determined as a valid number, elseFALSEis returned
-
isUnknown
protected CreditCardValidator.CreditCard isUnknown(java.lang.String creditCardNumber)
Can be used (subclassed) to extend the test with a credit card not yet known by the validator.- Parameters:
creditCardNumber- the credit card number as a string- Returns:
- The credit card id of the issuer
-
isChecksumCorrect
protected final boolean isChecksumCorrect(java.lang.String creditCardNumber)
Calculates the checksum of a credit card number using the Luhn algorithm (the so-called "mod 10" algorithm).- Parameters:
creditCardNumber- the credit card number for which the checksum should be calculated- Returns:
TRUEif the checksum for the given credit card number is valid, else returnFALSE- See Also:
- Wikipedie - Luhn algorithm
-
-