[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How can I validate a credit card number?

Posted on 1998-07-31
7
Medium Priority
?
580 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
[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
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
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
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 200 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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

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 article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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 tutorial demonstrates a quick way of adding group price to multiple Magento products.
Suggested Courses

649 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