Asp Programming Code Examples Asp > Strings Code Examples Credit Card Validation - Validates a credit card string Credit Card Validation - Validates a credit card string <SCRIPT LANGUAGE="VBScript" RUNAT=SERVER> Function Luhn(s) Dim i, iOdd, iEven, sInv, iNum i = 1 iOdd = 0 iEven = 0 sInv = StrReverse(s) Do While (i<=Len(sInv)) If Odd(i) Then iOdd = iOdd + CByte(Mid(sInv, i, 1)) Else iNum = 2 * CByte(Mid(sInv, i, 1)) If (iNum > 9) Then iEven = iEven + 1 + (iNum - 10) Else iEven = iEven + iNum End If End If i = i + 1 Loop Luhn = (i>1) And ((iOdd + iEven) Mod 10 = 0) End Function Function IsMaster(s) IsMaster = IsLen(s, 16) And IInside(CLng(Left(s, 2)), 51, 55) End Function Function IsVISA(s) IsVisa = IsPrefix(s, "4") And (IsLen(s, 13) Or IsLen(s, 16)) End Function Function IsAmex(s) IsAmex = IsLen(s, 15) And (IsPrefix(s, "34") Or IsPrefix(s, "37")) End Function Function IsDiners(s) IsDiners = IsLen(s, 14) And (IsPrefix(s, "36") Or _ IsPrefix(s, "38") Or IInside(CLng(Left(s, 3)), 300, 305)) End Function Function IsDiscover(s) IsDiscover = IsLen(s, 16) And IsPrefix(s, "6011") End Function Function IsRoute(s) IsRoute = IsLen(s, 15) And (IsPrefix(s, "2014") Or _ IsPrefix(s, "2049")) End Function Function IsJCB(s) IsJCB = (IsLen(s, 15) And (IsPrefix(s, "2131") Or _ IsPrefix(s, "1800")) Or (IsLen(s, 16) And _ IsPrefix(s, "3"))) End Function ' This routine checks for a valid credit card number, according ' to the prefixes and lengths below, and to Luhn's algorithm. ' ' Card Type Prefix Length ' ' MasterCard 51-55 16 ' VISA 4 13, 16 ' American Express 34, 37 15 ' Diner's Club 300-305, 36, 38 14 ' Carte Blanche 300-305, 36, 38 14 ' Discover 6011 16 ' enRoute 2014, 2049 15 ' JCB 3 16 ' JCB 2131, 1800 15 Function IsCardNumber(sCard, ccType) Dim ccMin, ccMax, minLen, maxLen, result, ccMaster,_ ccVISA, ccAmex, ccDiners, ccCarteB, ccDiscover,_ ccenRoute, ccJCB ccMin = 0 ccMax = 7 minLen = 13 maxLen = 16 result = False sCard = Trim(sCard) ccMaster = 0 ccVISA = 1 ccAmex = 2 ccDiners = 3 ccCarteB = 4 ccDiscover = 5 ccenRoute = 6 ccJCB = 7 If (IInside(ccType, ccMin, ccMax)) Then If (IInside(Len(sCard), minLen, maxLen)) Then Select Case ccType Case ccMaster result = IsMaster(sCard) Case ccVISA result = IsVISA(sCard) Case ccAmex result = IsAmex(sCard) Case ccDiners result = IsDiners(sCard) Case ccCarteB result = IsDiners(sCard) Case ccDiscover result = IsDiscover(sCard) Case ccenRoute result = IsRoute(sCard) Case ccJCB result = IsJCB(sCard) End Select End If End If If result Then result = Luhn(sCard) End If IsCardNumber = result End Function </SCRIPT>