We help IT Professionals succeed at work.

How can I validate a credit card number?

Medium Priority
604 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?
Comment
Watch Question

Commented:
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.

Commented:
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.

Commented:
I think I may have seen a APP to do that but it was also a long time ago

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

Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

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.

Commented:
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!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.