Categories

# How to test Credit Card numbers using Luhn’s algorithm

Almost all credit card numbers used are generated using Luhn’s algorithm. Luhn’s algorithm is a simple checksum formula to validate variety of numbers. We can use the same formula for checking whether a given credit card number may be valid or not.

Almost all credit card numbers used are generated using Luhn’s algorithm. Luhn’s algorithm is a simple checksum formula to validate variety of numbers. We can use the same formula for checking whether a given credit card number may be valid or not.

This can be used as a good technique to validate the credit card number in your web application. I found a simple function which validates any given number according to Luhn’s formula and gives the result to be true or false. The function is implemented in PHP and the primary source of this function is here.

```<?php
function is_valid_luhn(\$number) {
settype(\$number, 'string');
\$sumTable = array(
array(0,1,2,3,4,5,6,7,8,9),
array(0,2,4,6,8,1,3,5,7,9));
\$sum = 0;
\$flip = 0;
for (\$i = strlen(\$number) - 1; \$i >= 0; \$i--) {
\$sum += \$sumTable[\$flip++ & 0x1][\$number[\$i]];
}
return \$sum % 10 === 0;
}
```

This small function is very useful if you want to check the validity of the credit card number being provided before passing it to your payment mechanism.

## Sample Credit Card Numbers for testing

When I implemented this in my code, I also wanted to have some sample credit card numbers to test this. Sample credit card numbers are also very useful in many cases so here are the credit card numbers that I found.

 Credit Card Type Credit Card Number American Express 378282246310005 American Express 371449635398431 American Express Corporate 378734493671000 Australian BankCard 5610591081018250 Diners Club 30569309025904 Diners Club 38520000023237 Discover 6011111111111117 Discover 6011000990139424 JCB 3530111333300000 JCB 3566002020360505 MasterCard 5555555555554444 MasterCard 5105105105105100 Visa 4111111111111111 Visa 4012888888881881 Visa 4222222222222Note : Even though this number has a different character count than the other test numbers, it is the correct and functional number. Processor-specific Cards Dankort (PBS) 76009244561 Dankort (PBS) 5019717010103742 Switch/Solo (Paymentech) 6331101999990016

Original Source for these numbers is this.

I have just tried to sum up all the info that helped me with credit card numbers in one post. I hope this saves some time for other people.

## By Umair Jabbar

Umair Jabbar is an enthusiast, a Software Engineer / Web Developer who wants web to be simpler for everyone.

## 6 replies on “How to test Credit Card numbers using Luhn’s algorithm”

zeeshan Tahammulsays:

great!

[…] Almost all credit card numbers used are generated using Luhn’s algorithm. Luhn’s algorithm is a simple checksum formula to validate variety of numbers. We can use the same formula for checking whether a given credit card number may be valid or not.    PHP Read the original post on DZone… […]

Anonymoussays:

Testing this algorithm gives me an error on the:
76009244561  number.  I have tried on another online credit card checkers and it seems this number has an error based on the algo-luhn check.

Hey,

As i said it works for “almost all” credit card numbers. I am sure the credit card number you have given here is mot generated using Luhn’s Algo hence this technique here can’t verify it. But all major credit card providers use the same standard hence they can be verified using this technique.

Anonymoussays:

Ok, I have only say you because that is the third example starting at the end on your examples list.

I am porting your algorithm to C# here: http://www.ebys.es/en/85-technological/c/74-validating-credit-card-numbers-from-net.html

Kind regards.

I understand.

The credit card numbers are not there to test this algo though. they are just a list of credit card numbers that can be used for testing purposes of any sort.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Automated page speed optimizations for fast site performance