Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Luhn algorithm in Javascript. Check valid credit card numbers
// takes the form field 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.
var nCheck = 0, nDigit = 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) {
if ((nDigit *= 2) > 9) nDigit -= 9;
}
nCheck += nDigit;
bEven = !bEven;
}
return (nCheck % 10) == 0;
}
@mnadee123

This comment has been minimized.

Show comment Hide comment
@mnadee123

mnadee123 Feb 5, 2014

Nicely worked.

Nicely worked.

@JonDum

This comment has been minimized.

Show comment Hide comment
@JonDum

JonDum 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!

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.

Show comment Hide comment
@Maxhodges

Maxhodges Mar 5, 2014

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

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

@Brilliand

This comment has been minimized.

Show comment Hide comment
@Brilliand

Brilliand Jun 30, 2014

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

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

@MohamedAlaa

This comment has been minimized.

Show comment Hide comment
@MohamedAlaa

MohamedAlaa 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

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.

Show comment Hide comment
@manishatGit

manishatGit May 1, 2015

Quite helpful !!

Quite helpful !!

@javierverb

This comment has been minimized.

Show comment Hide comment
@javierverb

javierverb Jul 2, 2015

Thanks!!! great work! 😄

Thanks!!! great work! 😄

@kendepelchin

This comment has been minimized.

Show comment Hide comment
@kendepelchin

kendepelchin May 23, 2016

Great!

Great!

@mscheffer

This comment has been minimized.

Show comment Hide comment
@mscheffer

mscheffer Jul 18, 2016

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

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

@chetankmayank

This comment has been minimized.

Show comment Hide comment
@chetankmayank

chetankmayank Aug 29, 2016

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

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

@tkMageztik

This comment has been minimized.

Show comment Hide comment
@tkMageztik

tkMageztik Dec 28, 2016

awesome, thank you so much.

awesome, thank you so much.

@fullstacked

This comment has been minimized.

Show comment Hide comment
@fullstacked

fullstacked Jan 4, 2017

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

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

@volkerbohn

This comment has been minimized.

Show comment Hide comment
@volkerbohn

volkerbohn Mar 22, 2017

Nice work! Thanks!

Nice work! Thanks!

@anderconal

This comment has been minimized.

Show comment Hide comment
@anderconal

anderconal Mar 23, 2017

Awesome work, thanks!

Awesome work, thanks!

@ayushya

This comment has been minimized.

Show comment Hide comment
@ayushya

ayushya 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

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.

Show comment Hide comment
@marbogucki

marbogucki 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 ?

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.

Show comment Hide comment
@maxkremer

maxkremer Jun 5, 2017

ndigit is defined twice. jshint complains . line 12

ndigit is defined twice. jshint complains . line 12

@Natanagar

This comment has been minimized.

Show comment Hide comment
@Natanagar

Natanagar Sep 20, 2017

Nice work, thank you!

Nice work, thank you!

@dwhyte4

This comment has been minimized.

Show comment Hide comment
@dwhyte4

dwhyte4 Mar 3, 2018

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

dwhyte4 commented Mar 3, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment