Skip to content

Instantly share code, notes, and snippets.

@SleeplessByte
Created November 5, 2014 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SleeplessByte/841e2edf7640e30bdca0 to your computer and use it in GitHub Desktop.
Save SleeplessByte/841e2edf7640e30bdca0 to your computer and use it in GitHub Desktop.
Credit Card Detection
public static CardType fromCardNumber( String cardNumber ) {
if ( cardNumber == null )
return CardType.Null;
if ( cardNumber.matches( "^4[0-9]*$" ) )
return CardType.Visa;
if ( cardNumber.matches( "^5[1-5][0-9]*$" ) )
return CardType.MasterCard;
if ( cardNumber.matches( "^3[47][0-9]*$" ))
return CardType.Amex;
if ( cardNumber.matches( "^6(?:011|5)[0-9]*$" ))
return CardType.Discover;
return CardType.Null;
}
public static CardType Null = new CardType( "^[0-9]+$", "" );
public static CardType Visa = new CardType( "^4[0-9]{6,}$", "visa" );
public static CardType MasterCard = new CardType( "^5[1-5][0-9]{5,}$", "master card" );
public static CardType Amex = new CardType( "^3[47][0-9]{5,}$", "amex" );
public static CardType Discover = new CardType( "^6(?:011|5[0-9]{2})[0-9]{3,}$", "discover" );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment