[Home] [Puzzles & Projects] [Delphi Techniques] [Math topics] [Library] [Utilities]


Problem DescriptionHere's a simple little program to validate credit card numbers. Background & TechniquesAll credit card companies use a check digit to detect keying or scanning errors when credit card numbers are entered into a computer. UPCs (Universal Product Codes) also include a check digit but calculated in a slightly different manner. All common credit cards use a "Modulo 10" check digit algorithm. In this method all even digits, counting from the right, are doubled and the digits of the doubled numbers are totaled. To this is added the unused digits, i.e. the odd numbered positions starting from the right. The rightmost digit is the check digit and is included in this sum. If the result is a multiple of 10, the number is valid. The system catches any single data entry error and most transpositions. Clearly an error in any of the nondoubled positions will change the sum by an amount less than 10 and result in a detected error. Similarly an error in any of the doubled values will change the sum by an amount less than 10 and give an error. (0 through 9 doubled values add 0,2,4,6,8,1 [2*5=10, add 1+0] ,3 [2*6=12, add 1+2],5 [etc.],7, and 9 respectively). For transpositions, you can prove for yourself that any transposition except 0 and 9 or of two equal digits will change the sum by an amount greater than 0 but less than 10. 0 and 9 always add their own value regardless of position and thus cannot be detected if they are exchanged with another 0 or 9. The code provides an introduction to quite a few Delphi functions and operations: indexing through a string as an array, the length function for strings, the pos function to find blanks in the entered number, the delete function to remove them, the set inclusion test to make sure only digits are left, the copy function to extract a substring from a string, the ord function to get the number value of a character, and the mod and div operators to calculate the sum. Addendum October 6, 2003: A viewer wrote the other day asking if I could validate American Express card numbers. I don't have a Amex card, so can't be sure, but the version posted today does try, and does validate my Discover card OK, as well as "mod 11" ISBN (book) numbers and traditional "mod 10" check digit calculations. Addendum October 17, 2003: Thanks to viewer Brandon Hunt, the confusion over Amex and Discover cards has been cleared up. They do use prefix digits in creating check digits. All major cards use the same algorithm as described in the ANSI standard that governs credit cards: ANSI X4.13, "American National Standard for Financial Services  Financial Transaction Cards". The algorithm was developed by Hans Peter Luhn, one of the early pioneers developing techniques for making computers useful. He was granted the check digit patent in 1960, at age 64. More background information can be found at http://www.merriampark.com/anatomycc.htm Running/Exploring the ProgramSuggestions for Further Explorations

[Feedback] [Newsletters (subscribe/view)] [About me]Copyright © 20002017, Gary Darby All rights reserved. 