net.sourceforge.stripes.validation
Class CreditCardTypeConverter

java.lang.Object
  extended by net.sourceforge.stripes.validation.CreditCardTypeConverter
All Implemented Interfaces:
TypeConverter<String>

public class CreditCardTypeConverter
extends Object
implements TypeConverter<String>

A faux TypeConverter that validates that the String supplied is a valid credit card number. The Luhn algorithm is used in addition to valid credit card prefixes to determine if the string being converted qualifies as a credit card number. This DOES NOT check to see if an actual card with the specified number exists, only that it appears to be a valid card number.

If the credit card number is not valid a single error message will be generated. The error message is a scoped message with a default scope of converter.creditCard and name invalidCreditCard. As a result error messages will be looked for in the following order:

Since:
Stripes 1.5
Author:
Aaron Porter

Nested Class Summary
static class CreditCardTypeConverter.Type
           
 
Constructor Summary
CreditCardTypeConverter()
           
 
Method Summary
 String convert(String input, Class<? extends String> targetType, Collection<ValidationError> errors)
          Validates the user input to ensure that it looks like a valid credit card number.
static CreditCardTypeConverter.Type getCardType(String cardNumber)
          Determines the type of card from the card number passed in.
 void setLocale(Locale locale)
          Accepts the Locale provided, but does nothing with it since credit card numbers are Locale-less.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CreditCardTypeConverter

public CreditCardTypeConverter()
Method Detail

setLocale

public void setLocale(Locale locale)
Accepts the Locale provided, but does nothing with it since credit card numbers are Locale-less.

Specified by:
setLocale in interface TypeConverter<String>
Parameters:
locale - the locale that the TypeConverter will be converting from.

convert

public String convert(String input,
                      Class<? extends String> targetType,
                      Collection<ValidationError> errors)
Validates the user input to ensure that it looks like a valid credit card number.

Specified by:
convert in interface TypeConverter<String>
Parameters:
input - the String input, always a non-null non-empty String
targetType - realistically always String since java.lang.String is final
errors - a non-null collection of errors to populate in case of error
Returns:
the credit card with any non-numeric characters removed or null

getCardType

public static CreditCardTypeConverter.Type getCardType(String cardNumber)
Determines the type of card from the card number passed in.

Parameters:
cardNumber - the card number to check
Returns:
the type of card or null if the card number is invalid for all known card types


? Copyright 2005-2006, Stripes Development Team.