Solved

Credit Card Validation Code

Posted on 2001-08-03
13
11,966 Views
Last Modified: 2013-11-18
Hi folks,

Does anyone have a well-tested code to validate credit card information?  Any help would be greatly appreciated.

Jazon
0
Comment
Question by:piratepatrol
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 33

Accepted Solution

by:
hongjun earned 50 total points
ID: 6349615
Instructions
===============
The code assumes that you have the credit card type and number stored in session variables.

Checks credit card number for checksum, length and type. All useless characters are removed before validation.

Validates:

"V" VISA
"M" Mastercard/Eurocard
"A" American Express
"C" Diners Club / Carte Blanche
"D" Discover
"E" enRoute
"J" JCB
Returns:

checkcc=0 (Bit0) : card valid
checkcc=1 (Bit1) : wrong type
checkcc=2 (Bit2) : wrong length
checkcc=4 (Bit3) : wrong checksum (MOD10-Test)
checkcc=8 (Bit4) : cardtype unknown


checkcc.inc
=========
<%

function trimtodigits(tstring)
'removes all chars except of 0-9
      s=""
      ts=tstring
      for x=1 to len(ts)
            ch=mid(ts,x,1)
            if asc(ch)>=48 and asc(ch)<=57 then
            s=s & ch
            end if
      next
  trimtodigits=s
end function

function checkcc(ccnumber,cctype)
 
  ctype=ucase(cctype)
 
  select case ctype
    case "V"
      cclength="13;16"
      ccprefix="4"
    case "M"
      cclength="16"
      ccprefix="51;52;53;54;55"
    case "A"
      cclength="15"
      ccprefix="34;37"
    case "C"
      cclength="14"
      ccprefix="300;301;302;303;304;305;36;38"
    case "D"
      cclength="16"
      ccprefix="6011"
    case "E"
      cclength="15"
      ccprefix="2014;2149"
    case "J"
      cclength="15;16"
      ccprefix="3;2131;1800"
    case else
      cclength=""
      ccprefix=""
  end select
 
  prefixes=split(ccprefix,";",-1)
  lengths=split(cclength,";",-1)
  number=trimtodigits(ccnumber)
 
  prefixvalid=false
  lengthvalid=false
 
  for each prefix in prefixes
    if instr(number,prefix)=1 then
      prefixvalid=true
    end if
  next
   
  for each length in lengths
    if cstr(len(number))=length then
      lengthvalid=true
    end if
  next
 
  result=0
 
  if not prefixvalid then
    result=result+1
  end if
   
  if not lengthvalid then
    result=result+2
  end if  
 
  qsum=0
 
  for x=1 to len(number)
    ch=mid(number,len(number)-x+1,1)
    'response.write ch
    if x mod 2=0 then
      sum=2*cint(ch)
      qsum=qsum+(sum mod 10)
      if sum>9 then
        qsum=qsum+1
      end if
    else
      qsum=qsum+cint(ch)
    end if
  next
 
  'response.write qsum
  if qsum mod 10<>0 then
    result=result+4
  end if
 
  if cclength="" then
    result=result+8
  end if
 
  checkcc=result
 
end function
%>



checkcc.asp
============

<!-- #include file="checkcc.inc" -->

<%

'Check Credit Card Number

number = Session("CreditCardNo")
cctype = Session("PaymentType")

if number <> "" then
      chk=checkcc(number,cctype)
      
      if Session("Debug") = True then response.write "<br>Result: "
      
      if Session("Debug") = True then response.write "<br>" & chk
 
      select case chk
            case 0
                  if Session("Debug") = True then response.write "<P>Card is ok!"
            case 1
                  if Session("Debug") = True then response.write "<P>Wrong card type"
                  response.redirect("redeemerror.asp?id=CC1")
            case 2
                  if Session("Debug") = True then response.write "<P>Wrong length"
                  response.redirect("redeemerror.asp?id=CC2")
            case 3
                  if Session("Debug") = True then response.write "<P>Wrong length and card type"
                  response.redirect("redeemerror.asp?id=CC3")
            case 4
                  if Session("Debug") = True then response.write "<P>Wrong checksum"
                  response.redirect("redeemerror.asp?id=CC4")
            case 5
                  if Session("Debug") = True then response.write "<P>Wrong checksum and card type"
                  response.redirect("redeemerror.asp?id=CC5")
            case 6
                  if Session("Debug") = True then response.write "<P>Wrong checksum and length"
                  response.redirect("redeemerror.asp?id=CC6")
            case 7
                  if Session("Debug") = True then response.write "<P>Wrong checksum, length and card type"
                  response.redirect("redeemerror.asp?id=CC7")
            case 8
                  if Session("Debug") = True then response.write "<P>Unknown cardtype"
                  response.redirect("redeemerror.asp?id=CC8")
      end select
else
      response.write("<P>Please enter a card number")
end if

%>      


hongjun
0
 
LVL 33

Expert Comment

by:hongjun
ID: 6349637
0
 
LVL 18

Expert Comment

by:mgfranz
ID: 6349893
Most Credit Card processing services already have a CC validator in their code, if you use a company like iBill it's already there.

Nice code hongjun.
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 2

Expert Comment

by:bhh
ID: 6350569
Listening...
0
 
LVL 33

Expert Comment

by:hongjun
ID: 6350879
>>Nice code hongjun
I got it somewhere and I forgot about the link and so got to post the entire code here.

Anyway most companies might even have their own validator already. Either in the form of a dll or anything. Ask for it. But if you are doing for your own project, then you might need my above code.

hongjun
0
 
LVL 18

Expert Comment

by:mgfranz
ID: 6351438
Yeah, most of the processing companies already have a validator, the web store doesn't have to worry about it in most cases, unless they are doing their own processing...
0
 
LVL 1

Expert Comment

by:deestuar
ID: 6355010
Its called a Luhn check and there plenty of efficient code at

http://www.beachnet.com/~hstiles/cardtype.html
0
 
LVL 18

Expert Comment

by:mgfranz
ID: 6355837
Nice info deestuar, to bad all the links are dead or point to no such code...
0
 
LVL 1

Expert Comment

by:deestuar
ID: 6355868
In that case heres the info from that invalid link.

Credit Card Validation - Check Digits
This document outlines procedures and algorithms for Verifying the accuracy and validity of credit card numbers. Most credit card numbers are encoded with a "Check Digit". A check digit is a digit added to a number (either at the end or the beginning) that validates the authenticity of the number. A simple algorithm is applied to the other digits of the number which yields the check digit. By running the algorithm, and comparing the check digit you get from the algorithm with the check digit encoded with the credit card number, you can verify that you have correctly read all of the digits and that they make a valid combination.

Possible uses for this information:

When a user has keyed in a credit card number (or scanned it) and you want to validate it before sending it our for debit authorization.
When issuing cards, say an affinity card, you might want to add a check digit using the MOD 10 method.
1.Prefix, Length, and Check Digit Criteria
Here is a table outlining the major credit cards that you might want to validate.

CARD TYPE  Prefix  Length  Check digit algorithm  
MASTERCARD 51-55 16  mod 10
VISA 4 13, 16  mod 10
AMEX 34
37 15  mod 10
Diners Club/
Carte Blanche 300-305
36
38  14 mod 10
Discover 6011 16  mod 10
enRoute 2014
2149 15 any
JCB 3 16 mod 10
JCB 2131
1800 15  mod 10


2. LUHN Formula (Mod 10) for Validation of Primary Account Number
The following steps are required to validate the primary account number:


Step 1: Double the value of alternate digits of the primary account number beginning with the second digit from the right (the first right--hand digit is the check digit.)

Step 2: Add the individual digits comprising the products obtained in Step 1 to each of the unaffected digits in the original number.

Step 3: The total obtained in Step 2 must be a number ending in zero (30, 40, 50, etc.) for the account number to be validated.

For example, to validate the primary account number 49927398716:

Step 1:

        4 9 9 2 7 3 9 8 7 1 6
         x2  x2  x2  x2  x2
------------------------------
         18   4   6  16   2





Step 2: 4 +(1+8)+ 9 + (4) + 7 + (6) + 9 +(1+6) + 7 + (2) + 6

Step 3: Sum = 70 : Card number is validated

Note: Card is valid because the 70/10 yields no remainder.

The great folks at ICVERIFY are the original source of this data, I only formatted it in HTML.

If you are in the market, I wrote a set of FoxPro modules for Windows/Dos that interface nicely with ICVERIFY in a multi-user LAN setup. You just set up ICVERIFY on a single station, and all stations on the LAN can authorize credit cards with a single FOXBASE function call. Of course, you have to license ICVERIFY by the node, but it is very reasonable. I also wrote a couple of simple functions to perform pre-authorization, card screening, etc.

Here is a Microsoft Excel worksheet that will validate a number for you (useful for understanding the algorithm, it is in a .ZIP compressed format)

Eliot Jackson has written a set of VB 4 routines here.

Horace Vallas made a NeoWebScript (Tcl really) procedure that implements it.
Check it out at https://enterprise.neosoft.com/secureforms/hav/ 

Because I get at least a letter a week regarding this routine, here are some additional helpful notes:

Make sure that you:

have started with the rightmost digit (including the check digit) (figure odd and even based upon the rightmost digit being odd, regardless of the length of the Credit Card.) ALWAYS work right to left.
the check digit counts as digit #1 (assuming that the rightmost digit is the check digit) and is not doubled
double every second digit (starting with digit # 2 from the right)
remember that when you double a number over 4, (6 for example) you don't add the result to your total, but rather the sum of the digits of the result (in the above example 6*2=12 so you would add 1+2 to your total (not 12).
always include the Visa or M/C/ prefix.

E-Mail me at: hstiles@beachnet.com
Also, visit my home page or my POS page.

Last revised: 2/25/97
0
 
LVL 18

Expert Comment

by:mgfranz
ID: 6356001
No.... the links on that page are dead.  That page loads fine.
0
 
LVL 1

Expert Comment

by:deestuar
ID: 6356014
what links are dead I have tried some and they appear to work. What do you want ? Do you want some code i have seen javascript versions before ?

Stu
0
 
LVL 18

Expert Comment

by:mgfranz
ID: 6356023
Click on the VB4 (?) routines link, and the script at NeoWebScript is nowhere to be found.

I already have code thanks.
0
 
LVL 33

Expert Comment

by:hongjun
ID: 6357764
piratepatrol,
so any progresses?

hongjun
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
specific questions about shipping a coat 4 62
ASP server side get value 15 47
edit .asp files 5 48
VB script help 23 35
Often people are aiming at development of perfect Magento websites. Though, it is easier said than done. You know what’s much easier? To ruin everything. It can be done in seconds. Many of us experimented with design, tried to change some values dir…
E-commerce is quite a gambling world, and you should never entrust your business to a lucky chance. In order to outrun your competitors in a race to attract as many customers as possible, you need to have a well thought-out strategy under your belt.…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question