Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Luhn algorithm in Javascript. Check valid credit card numbers
// Takes a credit card string value and returns true on valid number
function valid_credit_card(value) {
// Accept only digits, dashes or spaces
if (/[^0-9-\s]+/.test(value)) return false;
// The Luhn Algorithm. It's so pretty.
let nCheck = 0, bEven = false;
value = value.replace(/\D/g, "");
for (var n = value.length - 1; n >= 0; n--) {
var cDigit = value.charAt(n),
nDigit = parseInt(cDigit, 10);
if (bEven && (nDigit *= 2) > 9) nDigit -= 9;
nCheck += nDigit;
bEven = !bEven;
}
return (nCheck % 10) == 0;
}
@mnadee123

This comment has been minimized.

Copy link

@mnadee123 mnadee123 commented Feb 5, 2014

Nicely worked.

@JonDum

This comment has been minimized.

Copy link

@JonDum JonDum commented Feb 12, 2014

Tested with all the test card numbers from http://www.paypalobjects.com/en_US/vhelp/paypalmanager_help/credit_card_numbers.htm

Worked with all. Good job!

@Maxhodges

This comment has been minimized.

Copy link

@Maxhodges Maxhodges commented Mar 5, 2014

anyone have the generate algorithm? I need to generate ID numbers with a check digit

@Brilliand

This comment has been minimized.

Copy link

@Brilliand Brilliand commented Jun 30, 2014

@Maxhodges - To generate a check digit, set bEven to true initially, and return (1000 - nCheck) % 10.

@MohamedAlaa

This comment has been minimized.

Copy link

@MohamedAlaa MohamedAlaa commented Jul 10, 2014

Execution time is very high: 35128.324ms

I've tried another Algorithm and it reported 0.059ms https://gist.github.com/ShirtlessKirk/2134376

@manishatGit

This comment has been minimized.

Copy link

@manishatGit manishatGit commented May 1, 2015

Quite helpful !!

@javierverb

This comment has been minimized.

Copy link

@javierverb javierverb commented Jul 2, 2015

Thanks!!! great work! 😄

@kendepelchin

This comment has been minimized.

Copy link

@kendepelchin kendepelchin commented May 23, 2016

Great!

@mscheffer

This comment has been minimized.

Copy link

@mscheffer mscheffer commented Jul 18, 2016

@mohamed execution time can't be 35 seconds !!

@chetankmayank

This comment has been minimized.

Copy link

@chetankmayank chetankmayank commented Aug 29, 2016

hi..can i get the code for aadhar card number validation using javascript and c#

@tkMageztik

This comment has been minimized.

Copy link

@tkMageztik tkMageztik commented Dec 28, 2016

awesome, thank you so much.

@fullstacked

This comment has been minimized.

Copy link

@fullstacked fullstacked commented Jan 4, 2017

This is not working with Amex cards. Can anyone pls guide me. Thanks.

@volkerbohn

This comment has been minimized.

Copy link

@volkerbohn volkerbohn commented Mar 22, 2017

Nice work! Thanks!

@anderconal

This comment has been minimized.

Copy link

@anderconal anderconal commented Mar 23, 2017

Awesome work, thanks!

@ayushya

This comment has been minimized.

Copy link

@ayushya ayushya commented Apr 20, 2017

Code Works great.

Although nDigit variable declared on Line 7 is not being used[ and can be removed ] since its been redeclared inside the for loop.

Correct me if wrong @DiegoSalazar

@marbogucki

This comment has been minimized.

Copy link

@marbogucki marbogucki commented May 26, 2017

I noticed one a little mistake in the end of the script;
If you type '0' or '00' or '0000000000' you will get true

My resolve to line 22:
return (nCheck !== 0) && (nCheck % 10) == 0

What do you think ?

@maxkremer

This comment has been minimized.

Copy link

@maxkremer maxkremer commented Jun 5, 2017

ndigit is defined twice. jshint complains . line 12

@Natanagar

This comment has been minimized.

Copy link

@Natanagar Natanagar commented Sep 20, 2017

Nice work, thank you!

@dwhyte4

This comment has been minimized.

Copy link

@dwhyte4 dwhyte4 commented Mar 3, 2018

can you explain why use the operators in nDigit -= 9; and bEven = !bEven;

@MacroMan

This comment has been minimized.

Copy link

@MacroMan MacroMan commented Jul 18, 2018

@dwhyte4
bEven = !bEven is a reversal, so bEven switches between true and false on each iteration of the loop, because each even digit is doubled as per the ISO/IEC 7812 spec.
nDigit -= 9; is to bring the number back between 1 and 9 if it's a double digit number (as per the ISO/IEC 7812 spec). This is the same effect as adding the 2 digits of the number together, which is what the spec says.

See https://en.wikipedia.org/wiki/Luhn_algorithm#Description for more info.

@craxidile

This comment has been minimized.

Copy link

@craxidile craxidile commented Jul 25, 2018

Good job!

@nic0latesla

This comment has been minimized.

Copy link

@nic0latesla nic0latesla commented Oct 16, 2018

Thanks bro

@kopollo

This comment has been minimized.

Copy link

@kopollo kopollo commented Oct 21, 2018

i used to this algorithm in php master card generator script on https://generator.creditcard/mastercard.php

@weider86

This comment has been minimized.

Copy link

@weider86 weider86 commented Feb 18, 2019

Thank you! It works nice!

I only added a validation to empty string (""), because it was returning true. Thank you a lot!

@pranith-bm-ai

This comment has been minimized.

Copy link

@pranith-bm-ai pranith-bm-ai commented Sep 6, 2019

I tried this out, but I realised I was looking rather naively at my problem. This works great at evaluating the complete card number.

I wanted to evaluate the card number while it is being typed, but I found there are challenges in determining the length of card number (can range from 13 to 19 digits) preemptively.

@DiegoSalazar

This comment has been minimized.

Copy link
Owner Author

@DiegoSalazar DiegoSalazar commented Sep 6, 2019

@pranith-bm-ai You can deduce from the first few numbers what kind of credit card it is and then you'll know what its length is supposed to be, see: https://www.freeformatter.com/credit-card-number-generator-validator.html#cardFormats

@altsanti

This comment has been minimized.

Copy link

@altsanti altsanti commented Oct 11, 2019

I would add the line "if (!value) return false;" on top, to also take care of the situation of an empty input (or input with spaces).

@emandeguzman

This comment has been minimized.

Copy link

@emandeguzman emandeguzman commented Apr 23, 2020

I tested against https://www.paypalobjects.com/en_US/vhelp/paypalmanager_help/credit_card_numbers.htm

76009244561 did not return true

But I'm not sure if this number is really valid. all luhn verification code seem to fail when I use this number.

@JonHualde

This comment has been minimized.

Copy link

@JonHualde JonHualde commented Jun 11, 2020

Working well, thank you very much Diego!

@sureshshivale

This comment has been minimized.

Copy link

@sureshshivale sureshshivale commented Jul 17, 2020

Thank you! Nice work !
I'm Getting false result for Dankort (PBS) | 76009244561

Can someone answer for me on this ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.