How to Validate a Credit Card Number in JavaScript (Luhn Algorithm)

preview_player
Показать описание
How to Validate a Credit Card Number in JavaScript (Luhn Algorithm)

Greetings, today I shall be showing you how to use the Luhn algorithm to validate a credit card number or debit card, using JavaScript.

The Luhn algorithm is a simple checksum formula used to validate the integrity of identification numbers such as credit card numbers.
The Luhn algorithm works by performing the following steps:
Double every other digit in the number, starting with the second-to-last digit and moving left.

If a doubled number is greater than 9, you mod the number by 10 and add 1 onto the remainder.

We then add up all the digits.

We then mod the total by 10. If the sum is a multiple of 10, we return true as it is a valid credit card, if not then false as the credit card is invalid.

Thanks for watching this JavaScript credit card number validation tutorial.

How to Validate a Credit Card Number in JavaScript (Luhn Algorithm)
Рекомендации по теме
Комментарии
Автор

Hey, thanks for the video but I'm also lost once you start writing the function. for (let i = ... I dont know why you're subtracting 2, why you're starting from the right. You're also applying the Luhn algorithm differently to others. I know the outcome is the same but maybe your could explain that too. Thanks for the content. keep up the good work!

danielnicholson
Автор

Hello, I like the video, but I think you should do a few things to help me and other people understand more.
1. explain the Luhn Algorithm. I had to learn the algorithm by myself, and a quick explanation in this video would save time for people.
2. explain everything you are doing. For myself, and newer coders, I barely understood why you were doing what you were doing. For instance, why are we splitting? And how exactly does the what you're doing pertain to the Luhn algorithm?
3. Explain your names. I don't know what 'creditcardint' means. It would help myself if you were to explain these names to me.
4. you could have a bit longer explanations to some things and not rush through. Make the video a bit longer and slow down a bit, cause it's kind of a lot to process when learning a new thing in a language I barely know, and I think you give people here more credit than they're due. For instance, what does 'mod' mean for % ? I would've liked a deeper explanation for that.

Other than those things, keep up the good work, and I'll check back to see if you've made another video on this.

prophetgproductions
Автор

In your If statement for the tempValue, could you also use tempValue - 9?

danbuild
Автор

Also, why does the for loop start on a value that should be multiplied by itself? - Arr.length - 2 would read as though you are targeting the third value in from the end of the array:

arr = [1, 2, 3, 4, 5] -> length - 2 = 3??

danbuild