Validating a credit card
When actually processing a transaction, you'd also want to check the customer name, expiration date, etc. The code demonstrated here is really just a first step, meant to help the user by checking for typos or invalid characters.
It can also help you by eliminating processing on the server for card numbers that are obviously not valid.
This luhn check is simply used to prevent transpositional errors though.
It is useful as a sanity check prior to submitting card numbers to a payment gateway, but not suitable to absolutely validate whether a number is a valid card number.
They follow whatever coding scheme that particular card company chooses.
In other words, for this example, we don't add 2 10 ... instead: 2 1 0 4 6 2 2 0 1 8 8 1 4 6 1 0 4 6 2 2 = 60 You may have noticed that the checksum digit (2) is included in the sum.By performing the same calculations, you can determine if the number matches the checksum digit. Note that if a number passes this test, it does not necessarily mean that it is definitely valid.Usually, other checks should be made to determine if the card number is acceptable.CARD$ is the credit card, numeric portion only W is a working variable (temporary to hold the number before added to the accumulator) X is the position of the digit being worked upon, starting at the end of the card number (-1) and working backwards.The ever-so-important difference is that the adjusted card number sum is multiplied by 9 and run through a modula 10 check.