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?

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.

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.

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

Richard2000Author Commented:

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.

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

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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.