[How-To] Check if a Number Is A Valid Credit Card Number
The hack in this post makes use of Luhn’s algorithm which was developed by Hans Peter Luhn who was awarded US Patent 2950048 (“Computer for Verifying Numbers”) for the technique in 1960. In Practice, Using the Luhn’s algorithm, the last digit of the credit card number is arrived at. The same algorithm can be used to check if a number is actually a valid credit card number.
Credit card numbers are usually of 16 digits, however, they may go upto 19 digits. In order to verify if a number is a valid credit card number, follow the steps below.
In order to arrive at the answer using Luhn’s Algorithm, we will need a 4 row table with number of column’s equal to the number of credit card digits ( most probably 16 or more in special cases)
1.) Write the credit card number in the first row. Lets take the example as 5189 3691 3883 7916 as a credit card number which needs to be verified as a correct number.
2.) In the second row of the table, alternatively multiply the digit by 2.
5 1 8 9 3 6 9 1 3 8 8 3 7 9 1 6 5 x 2 = 10 1 8 x 2 = 16 9 3 x 2 = 6 6 9 x 2 = 18 1 3 x 2 = 6 8 8 x 2 = 16 3 7 x 2 = 14 9 1 x 2 = 2 6 10-9=1 1 16-9=7 9 6 6 18-9=9 1 6 8 16 – 9 = 7 3 14 – 9 = 5 9 2 6 1 1 7 9 6 6 9 1 6 8 7 3 5 9 2 6 3.) In the third row, force all digits to be less than 10, by subtracting 9 where necessary.
4.) Enter the digits obtained after the subtraction in fourth row.
5.) Add up all the digits in fourth row. If the end result is divisible by 10, the number is a valid credit card number. In our case, we got 86, which is not divisible by 10 and hence 5189 3691 3883 7916 is not a valid credit card number.
If you are willing to learn more about how credit card numbers are formed and what is the significance of each digit, i would suggest that you go through this article.
You can follow me on Twitter at http://twitter.com/vaibhav1981
Do stay tuned to Technofriends for more, one of the best ways of doing so is by subscribing to our feeds. You can subscribe to Technofriends feed by clicking here.
Related posts:
- [How-To] Create US iTunes Account Without Providing Any Credit Card Information
- [Java] How To Check if a String is a Valid Number?
- [How-To] Get Your Credit Information Report from CIBIL (India only)
- [India] 11 Digit Mobile Numbers might not happen ( Unlocking 2-9 series)
- [Discounts] Get Free $4 “Video on Demand” Credit from Amazon (Until Jan 3rd 2010)
Creepy indeed….
I used to try all permutations, combinations on prepaid cards, but that seemed to fail often…
I think there some different system for India, which obviously we dont know…
Hi Harshad
this is a global way of figuring out a valid credit card number. You can try out with your credit card information.
Python implementation below. Enjoy
def check_luhn( card_number ):
“”" card_number is a string of usually 16 characters “”"
sum = 0
num_digits = len(card_number)
oddeven = num_digits & 1
for count in range(0, num_digits):
digit = int(card_number[count])
if not (( count & 1 ) ^ oddeven ):
digit = digit * 2
if digit > 9:
digit = digit – 9
sum = sum + digit
return ( (sum % 10) == 0 )
It works the same way, but I learned it instead of subtracting 9 from the product if it was greater than 10, you simply add all the digits. “10″ = 1 + 0; “14″ => 1 + 4, etc.
Algorithm in Javascript, with explanation:
http://typicalprogrammer.com/?p=4
[...] [How-To] Check if a Number Is A Valid Credit Card Number … [...]
[...] [How-To] Check if a Number Is A Valid Credit Card Number … [...]
[...] How to tell if a credit card number is valid. Link [...]
Great post, but actually most are 16 digits or less, not more. All American Express numbers are only 15 digits in length. 17 or more digits are exceptionally rare, in fact I’ve never seen one and I’ve administered a database with literally millions of credit card numbers.
[...] [How-To] Check if a Number Is A Valid Credit Card Number | Technofriends Great tool. (tags: creditcard programming webdevelopment ecommerce algorithms reference validation) [...]
[...] [How-To] Check if a Number Is A Valid Credit Card Number Posted on Monday, February 9th, 2009 in How-To – Comments: (11) The hack in this post makes use of Luhn’s algorithm which was developed by Hans Peter Luhn who was awarded US Patent 2950048 (”Computer for Verifying Numbers”) for the technique in 1960. In Practice, Using the Luhn’s algorithm, the last digit of the credit card number is arrived at. The same algorithm can be used to check if a number is actually a valid credit card number. [...]
[...] [How-To] Check if a Number Is A Valid Credit Card Number … [...]
how would i check if a credit card is still valid…