Solved

How can I validate a credit card number?

Posted on 1998-07-31
7
554 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Every business owner understands the significance of online customer reviews and the impact it can have on sales and revenues. With technology advancing at such a rapid pace, getting online reviews has never been easier, especially when many regions…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now