Created
November 5, 2014 22:08
-
-
Save SleeplessByte/841e2edf7640e30bdca0 to your computer and use it in GitHub Desktop.
Credit Card Detection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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