NOTE: This article has been updated and moved to: Validating Credit Card Numbers. NET to process online credit card orders, it is a good idea if you can perform some sort of validation on the credit card number before submitting it to your processor.I recently had to write some code to process credit card orders and thought I’d share a bit of my code.Fortunately, credit card numbers are created in a way that allows for some basic verification.
In fact, It’s possible that the card number is mistyped in such a way that it just happens to pass verification.
But it does catch most typing errors and reduces bandwidth by catching those errors before trying to actually process the credit card.
To validate a credit card number, you start by adding the value of every other digit, starting from the right-most digit and working left.
Next, you do the same thing with the digits skipped in the first step, but this time you double the value of each digit and add the value of each digit in the result.
Finally, you add both totals together and if the result is evenly divisible by 10, then the card has passed validation.
Of course, this would be clearer with a bit of code and Listing 1 shows my Is Card Number Valid method. The Is Card Number Valid method assumes that all spaces and other non-digit characters have been stripped from the card number string.
This is a straight forward task but Listing 2 shows the method I use for this.
Listing 2: Removing all non-digit characters from a credit card number.
You will also be able to reduce bandwidth if you can avoid trying to submit a card that is not supported by the business.