Skip to content

Instantly share code, notes, and snippets.

@jmarmolejos
Last active January 2, 2016 14:59
Show Gist options
  • Save jmarmolejos/8320190 to your computer and use it in GitHub Desktop.
Save jmarmolejos/8320190 to your computer and use it in GitHub Desktop.
Quick port of the function used here to detect the credit card type: http://webstandardssherpa.com/reviews/auto-detecting-credit-card-type
// Quick port of the function used here to detect the credit card type:
// http://webstandardssherpa.com/reviews/auto-detecting-credit-card-type
public static string GetCardType(string Number)
{
//start without knowing the credit card type
var result = "unknown";
//first check for MasterCard
if (Regex.Match(Number, @"^5[1-5]").Success)
{
result = "mastercard";
}
//then check for Visa
else if (Regex.Match(Number, @"^4").Success)
{
result = "visa";
}
//then check for AmEx
else if (Regex.Match(Number, @"^3[47]").Success)
{
result = "amex";
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment