This project is a JavaScript-based solution to check the validity of credit card numbers using the Luhn algorithm. It optimizes the verification process by utilizing JavaScript functions and loops, allowing for the validation of multiple credit card numbers simultaneously.
- Determine whether a given set of credit card numbers are valid or not.
- The provided credit card numbers are organized into arrays, each with specific prefixes denoting their status: valid for valid numbers, invalid for invalid numbers, and mystery for numbers with uncertain validity.
- Each time an array is checked, it should return
valid
orinvalid
.
To use this credit card validator, follow these steps:
- Clone the repository to your local machine.
- Open the main.js file in your preferred JavaScript environment.
- Input the number you want to validate in
cardNumber()
. - Run the
cardNumber()
function to initiate the validation process. - At the same time, the
generateCardNumber()
function will run and print out a valid card number.
The validation process employs the Luhn algorithm, also known as the "modulus 10" or "mod 10" algorithm. This algorithm verifies the authenticity of a credit card number by performing a specific process on its digits.
Starting from the left, excluding the last digit or also known as "check digit", double every other digit. If the number is greater than 9 after doubling it, subtract 9 from its value. Sum up all the digits in the credit card number. If the result is divisible by 10, the credit card number is considered valid. Here is a visual outline of the steps:
The project has been extended to include additional features:
Users can input a credit card number and receive instant feedback on its validity. The cardNumber()
function takes a credit card number as input and prints whether the card number is valid Valid, [ Card Company ]
or not Invalid, [ Card Company ]
along with the card company's name.
The project can also generate a valid credit card number for any of the accepted credit card companies. The generateCardNumber()
function generates card numbers for Amex, Visa, Mastercard, or Discover and is validated through Luhn check.
I find myself immersed in solving this project even when I'm not facing my computer. I needed to understand the Luhn algorithm, as this was my first time hearing this special math. And because this is my first project in JavaScript, I knew I would face setbacks during the development.
Sometimes, I got stuck and things got confusing, especially in creating the card generator function. Thankfully, I got answers from helpful resources in Stack Overflow. Getting to deeply understand functions through reading in MDN Docs fine-tuned the code I wrote.
Feel free to explore, use, and extend this project to suit your credit card validation needs!
@Aug 2023