- All Visa card numbers start with a 4.
- All Visa card numbers are between 13 and 16 digits.
- There can only be integers in a Visa card number.
- They can all be validated through the Luhn algorithm.
Using a set of conditional arguments I could quickly cross off the first three criteria in the list above. However the Luhn formula required some deeper thought process to solve. For those unfamiliar with the algorithm for Visa card numbers, these are the steps.
- Multiply each other digit from the right to the left with 2, skipping the rightmost.
- Retrieve the digit sum of the number calculated from step 1.
- To the sum add the remaining digits, skipping the rightmost.
- Validate the result by summing the total of the number and rightmost digit, followed by modulating by ten (%10).
- If the remainder is 0 the number is valid.
Using these instuctions I constructed three for loops; one for the multiplication, one for calculating the digit sum and finally one for summarizing the remaining digits with the results from the first two loops. Lastly returning a Boolean based on the final validation result.
Throughout the program the user is notified with alert dialogs explaining each mistake or if the validation process was successful.