CFLib.org – Common Function Library Project

getCardType(cardNumber)

Last updated November 03, 2005

author

Dave Cordes

Version: 1 | Requires: CF5 | Library: UtilityLib

Description:
Returns the card type of a card number. Will return "Unknown" if the card type cannot be detected.

Return Values:
Returns a string.

Example:

<cfoutput>
What card type is 4111111111111111? #GetCardType(4111111111111111)#
</cfoutput>

Parameters:

Name Description Required
cardNumber The credit card number. Yes

Full UDF Source:

/**
 * Returns the card type of a card number.
 * 
 * @param cardNumber      The credit card number. (Required)
 * @return Returns a string. 
 * @author Dave Cordes (dcordes@apoktechnology.com) 
 * @version 1, November 3, 2005 
 */
function getCardType(cardNumber) {
    var CardType = "Unknown";
    var CardLength = Len(cardNumber);
    
    if ((CardLength eq 15) and (((Left(cardNumber, 2) is "34")) or ((Left(cardNumber, 2) is "37")))) CardType = "American Express";
    if ((CardLength eq 14) and (((Left(cardNumber, 3) gte 300) and (Left(cardNumber, 3) lte 305)) or (Left(cardNumber, 2) is "36") or (Left(cardNumber, 2) is "38"))) CardType =  "Diner's Club";
    if ((CardLength eq 16) and (Left(cardNumber, 4) is "6011")) CardType =  "Discover Card";
    if ((CardLength eq 16) and (Left(cardNumber, 2) gte 51) and (Left(cardNumber, 2) lte 55)) CardType =  "MasterCard";
    if (((CardLength eq 13) or (CardLength eq 16)) and (Left(cardNumber, 1) is "4")) CardType =  "Visa";
    
    return CardType;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson