Introduction
This is the article which tells you how to validate “Credit Card Number”. This articles doesn’t guarantee that the Credit Card Number exist what it tells that it is the valid number or not.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p>
Information
Credit Card numbers are (most times) 13 to 16 digit numbers which are protected by a special numerical check, called Luhn check.
The Luhn algorithm or Luhn formula, also known as the "modulus 10" or "mod 10" algorithm, was developed in the 1960s as a method of validating identification numbers. It is a simple checksum formula used to validate a variety of account numbers, such as credit card numbers and Canadian Social Insurance Numbers. Much of its notoriety comes from credit card companies' adoption of it shortly after its creation in the late 1960s by IBM scientist Hans Peter Luhn (1896–1964).
The algorithm is in the public domain and is in wide use today. It is not intended to be a cryptographically secure hash function; it protects against random error, not malicious attack. Most credit cards and many government identification numbers use the algorithm as a simple method of distinguishing valid numbers from collections of random digits.<o:p>
Contents <o:p>
1 Informal explanation<o:p>
<o:p>
2 Algorithm<o:p>
<o:p>
3 Example<o:p>
<o:p>
4 Code<o:p>
<o:p>
Informal Explaination<o:p>
The formula generates a check digit, which is usually appended to a partial account number to generate the full account number. This account number must pass the following algorithm (and the check digit chosen and placed so that the full account number will):<o:p>
Starting with the second to last digit and moving left, double the value of all the alternating digits. For any digits that thus become 10 or more, add their digits together. For example, 1111 becomes 2121, while 8763 becomes 7733 (from (1+6)7(1+2)3). <o:p>
Add all these digits together. For example, 1111 becomes 2121, then 2+1+2+1 is 6; while 8763 becomes 7733, then 7+7+3+3 is 20. <o:p>
If the total ends in 0 (put another way, if the total modulus 10 is 0), then the number is valid according to the LUHN formula, else it is not valid. So, 1111 is not valid (as shown above, it comes out to 6), while 8763 is valid (as shown above, it comes out to 20). <o:p>
In the two examples above, if a check digit was to be added to the front of these numbers, then 4 might be added to 1111 to make 41111, while 0 would be added to 8763 to make 08763. It is usually the case that check digits are added to the end, although this requires a simple modification to the algorithm to determine an ending check digit given the rest of the account number.
Algorithm
The algorithm proceeds in three steps. Firstly, every second digit, beginning with the next-to-rightmost and proceeding to the left, is doubled. If that result is greater than nine, its digits are summed (which is equivalent, for any number in the range 10 though 18, of subtracting 9 from it). Thus, a 2 becomes 4 and a 7 becomes 5. Secondly, all the digits are summed. Finally, the result is divided by 10. If the remainder is zero, the original number is valid.<o:p>
The following is wikicode, a proposed pseudocode for use in many articles.<o:p>
<o:p>
function checkLuhn(string purportedCC) {<o:p>
int sum := 0<o:p>
int nDigits := length(puportedCC)<o:p>
int parity := nDigits modulus 2<o:p>
for i from 0 to nDigits - 1 {<o:p>
int digit := integer(purportedCC[i])<o:p>
<o:p>
if i modulus 2 = parity<o:p>
digit := digit × 2<o:p>
if digit > 9<o:p>
digit := digit - 9 <o:p>
sum := sum + digit<o:p>
}<o:p>
return (sum modulus 10) = 0<o:p>
}<o:p>
<o:p> |
<o:p>
Example
Consider the example identification number 456-565-654. The first step is to double every other digit, starting with the second-to-last digit and moving left, and sum the digits in the result. The following table shows this step (highlighted rows indicating doubled digits):
Digit Doubled Sum of digits <o:p>
4 4 4 <o:p>
5 10 1 <o:p>
6 6 6 <o:p>
5 10 1 <o:p>
6 6 6 <o:p>
5 10 1 <o:p>
6 6 6 <o:p>
5 10 1 <o:p>
4 4 4 <o:p>
Sum: 30 <o:p>
<o:p>
The sum of 30 is divided by 10; the remainder is 0, so the number is valid.<o:p>
Code <o:p>
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click<o:p>
Dim sum As Int32 = 0<o:p>
Dim cdigit As Char<o:p>
Dim i, digit As Int32<o:p>
Dim nDigit As Int32<o:p>
Dim parity As Int32<o:p>
nDigit = TextBox1.TextLength<o:p>
parity = nDigit Mod 2<o:p>
For i = 0 To nDigit - 1<o:p>
cdigit = TextBox1.Text.Chars(i)<o:p>
digit = cdigit.GetNumericValue(cdigit)<o:p>
If i Mod 2 = parity Then<o:p>
digit = digit * 2<o:p>
End If<o:p>
If digit > 9 Then<o:p>
digit = digit - 9<o:p>
End If<o:p>
sum = sum + digit<o:p>
Next<o:p>
If sum Mod 10 = 0 Then<o:p>
MsgBox("Approved")<o:p>
Else<o:p>
MsgBox("Not Approved")<o:p>
End If<o:p>
End Sub<o:p>
<o:p> |
Conclusion
I think it is very useful for those who are in learning process and want to learn and try different things.