Solved

How can I validate a credit card number?

Posted on 1998-07-31
7
561 Views
Last Modified: 2010-08-05
I am trying to write a function in VB 4 which takes a credit card number and returns...

* Whether the card number is valid or not.
* The type of credit card (Visa, MasterCard, Discover, American Express etc).

I know there is a mathematical algorithm which can check for valid credit card number patterns.  Can anyone provide me with a routine which does the above and/or an algorithm to do this please?  I think that the card type can easily be detected by looking at the first digit of a credit card number, but which value represents which card type?
0
Comment
Question by:Richard2000
7 Comments
 
LVL 2

Expert Comment

by:shchuka
ID: 1467356
It's simple to determine the credit card type by the last 4 digits of the number.  There's not a mathematical algorithm to determine the validity of the card - otherwise it would be very simple to fraud the cards and all that stuff.

If you need this for a commercial application, there's an OCX control for credit card validation and even charging amounts to it.  I don't remember of hand, but the ad for it has been in every issue of VB programmer's journal since about two years ago.
0
 
LVL 6

Expert Comment

by:alamo
ID: 1467357
Actually, it's the first 4 digits which specify credit card type. And there is a way to do simple validation of the number - if I recall correctly it's a multiplier such as 1212121212121212 which gets multiplied (1 digit at a time) and the results added. If the result is 0 then the card is a valid number (which doesn't eman it's a valid card of course, this check is used to quickly detect most keying errors without doing a whole validation). The multiplier differs from card to card.

I used to have some info on this (I quoted sucha  project a long time ago), I'll try to find it.
0
 
LVL 1

Expert Comment

by:MAVERICK
ID: 1467358
I think I may have seen a APP to do that but it was also a long time ago

0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 2

Expert Comment

by:percosolator
ID: 1467359
I thought it was the first digit.

4 - Discover
5 - Mastercard
.

We're using a product called PC-Charge with VB.  It's an ActiveX OCX that performs credit card transactions, credit card number validation....


Go Software, Inc.
42 W. Montgomery Crossroad, Suite O
Savanna, GA 31406
(912) 925-4048

0
 
LVL 6

Accepted Solution

by:
alamo earned 50 total points
ID: 1467360
Here's a function to check that the credit card number passes the Mod10 validation algorithm, which all except "EnRoute" do:

Public Function Mod10Check(sNum As String) As Boolean
' Check a credit card number using mod10
Dim i, iDigit, iTotal, iDoubleFlag As Integer

    For i = Len(sNum) To 1 Step -1
        iDigit = Asc(Mid$(sNum, i, 1)) - 48
        If iDigit < 0 Or iDigit > 9 Then
            Mod10Check = False
            Exit Function
        End If
        If iDoubleFlag Then
            iDigit = iDigit + iDigit
            If iDigit > 9 Then iDigit = iDigit - 9
            iDoubleFlag = False
        Else
            iDoubleFlag = True
        End If
        iTotal = iTotal + iDigit
    Next
    Mod10Check = (iTotal Mod 10 = 0)
End Function

As to card type - you in some cases need to look at more than the first digit. Here's a list, I have seen these values in more than one place so they seem to be reliable.
Type            Prefix Digits   Length
MASTERCARD      51-55           16
VISA            4               13 or 16
AMEX            34,37           15
Diners Club/Carte Blanche
                300-305,36,38   14
Discover        6011            16
enRoute         2014,2149       15 no checksum
JCB             3               16
JCB             2131,1800       15

Good luck!
0
 

Author Comment

by:Richard2000
ID: 1467361
Thank you for your answer and comments.  I was wondering if alamo (or anyone else) has any more information about multipliers which can help check for valid numbers?

The Mod10 algorithm is good, but using multipliers would probably be a little more robust.  I know there is no way of ever being 100% sure that a card is valid, but I am looking for one or more algorithms which can detect keying errors etc. for major credit cards.  I don't really want to splash out on a commercial OCX to do this because I'm only interested in relatively simple validation of numbers rather than charging cards etc.  There are several shareware programs around which do look at credit card number patterns to detect keying errors, so it is possible.
0
 
LVL 6

Expert Comment

by:alamo
ID: 1467362
I am sorry if I wasn't clear - the code I posted to calculate the Mod10 total already incorporates the multipliers for all cards (except enRoute, which has no check digit).

When I originally posted that there were different multipliers, I was only half right. The actual multipliers are different for some cards, but only because the cards have different digit lengths. However, the algorithm used to generate the multiplier is the same for all cards: the rightmost digit is a 1, the next digit to the left is 2, and then the digits alternate. For example, Amex is 121212121212121 and Mastercard is 2121212121212121.

The original reason for the check digit was to cheaply catch simple manual entry errors, such as a single digit entered incorrectly and two adjacent digits transposed. The alternating 1-2 multiplier does a decent job of that.

Hope this helps!
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

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.…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

786 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