CFLib.org – Common Function Library Project

IsCreditCard(ccNo [, cardType])

Last updated August 17, 2004
Download UDF

author

Nick de Voil                                      Nick de Voil

Version: 4 | Requires: ColdFusion 5 | Library: StrLib

 
Rated 0 time(s). Average Rating: 0

Description:
Returns TRUE if the given credit card number is well-formed according to the industry-standard Luhn algorithm, and (optionally) conforms with basic formatting rules applicable to the specific card type, e.g. Visa, Mastercard. <strong> CAUTION: Note that this does not prove that the card has not been revoked, stolen etc.</strong> Non-numeric characters in the card number string are simply ignored.

Return Values:
Returns a boolean.

Example:

<CFOUTPUT>
Is the card number 5418590012345679 valid? #IsCreditCard("5418590012345679")#
<P>
Is the card number 5418590012345679 a valid Mastercard? #IsCreditCard("5418590012345679", "MASTERCARD")#
<P>
Is 541A590012345679 valid? #IsCreditCard("541A590012345679")#
<P>
Is the card number 541 8590 01234 5679 valid? #IsCreditCard("541 8590 01234 5679")#

</cfoutput>

Parameters:

Name Description Required
ccNo The credit card number. Yes
cardType One of: AMEX, DINERS, DISCOVER, MASTERCARD, VISA No

Full UDF Source:

<cfscript>
/**
* Returns TRUE if the string is a valid credit card number.
* Modded by RCamden - Check for any non numeric and return false.
* Modded by Author - fixed mastercard checking
* Updated to use [:digit:] and allow spaces
* Corrected nondigit check
*
* @param ccNo      The credit card number. (Required)
* @param cardType      One of: AMEX, DINERS, DISCOVER, MASTERCARD, VISA (Optional)
* @return Returns a boolean.
* @author Nick de Voil (nick@devoil.com)
* @version 4, August 17, 2004
*/

function IsCreditCard(ccNo)
{
    var rv = "";
    var str = "";
    var chk = 0;
    var ccln = 0;
    var strln = 0;
    var i = 1;

    if(reFind("[^[:digit:]]",ccNo)) return FALSE;
ccNo = replace(ccNo," ","","ALL");
    rv = Reverse(ccNo);
    ccln = Len(ccNo);
    if(ccln lt 12) return FALSE;
    for(i = 1; i lte ccln; i = i + 1) {
        if(i mod 2 eq 0) {
            str = str & Mid(rv, i, 1) * 2;
        } else {
            str = str & Mid(rv, i, 1);
        }
    }
    strln = Len(str);
    for(i = 1; i lte strln; i = i + 1) chk = chk + Mid(str, i, 1);
    if((chk neq 0) and (chk mod 10 eq 0)) {
        if(ArrayLen(Arguments) lt 2) return TRUE;
        switch(UCase(Arguments[2])) {
        case "AMEX":        if ((ccln eq 15) and (((Left(ccNo, 2) is "34")) or ((Left(ccNo, 2) is "37")))) return TRUE; break;
        case "DINERS":        if ((ccln eq 14) and (((Left(ccNo, 3) gte 300) and (Left(ccNo, 3) lte 305)) or (Left(ccNo, 2) is "36") or (Left(ccNo, 2) is "38"))) return TRUE; break;
        case "DISCOVER":    if ((ccln eq 16) and (Left(ccNo, 4) is "6011")) return TRUE; break;
        case "MASTERCARD":    if ((ccln eq 16) and (Left(ccNo, 2) gte 51) and (Left(ccNo, 2) lte 55)) return TRUE; break;
        case "VISA":        if (((ccln eq 13) or (ccln eq 16)) and (Left(ccNo, 1) is "4")) return TRUE; break;
        default: return TRUE;
        }
    }
    return FALSE;
}
</cfscript>

Search CFLib.org


Latest Additions

Shawn Porter Shawn Porter added
DeMoronize
3 hour(s) ago

Chris Carey Chris Carey added
readPropertiesFi...
1 day(s) ago

Randy Johnson Randy Johnson added
lastDayofWeek
3 day(s) ago

Frank Marion Frank Marion added
sitemapPing
7 day(s) ago

Top Rated

James Sleeman                                     QuickSort
Rated 5.0, 3 time(s)

Barney Boisvert indentXml
Rated 5.0, 3 time(s)

Nathan Dintenfass                                 queryColumnsToSt...
Rated 5.0, 3 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 3 time(s)

Created by Raymond Camden / Design by Justin Johnson