Platforms to show: All Mac Windows Linux Cross-Platform
FAQ.Do you have code to validate a credit card number?
Answer: You can check the checksum to tell if a credit card number is not valid.
Here's some code that will validate the checksum for a credit card. It works for Visa, MasterCard, American Express and Discover. Not sure about others, but I imagine they use the same basic algorithm. Of course, this doesn't actually mean that the credit card is valid, it's only useful for helping the user catch typos.
The above code doesn't have any error checking and it expects that the credit card number will be entered without spaces, dashes or any other non-numeric characters. Addressing those issues will be an exercise left to the reader. :)
(From Mike Stefanik)