Skip to content

Instantly share code, notes, and snippets.

@bhaskarmelkani
Created April 16, 2016 12:03
Show Gist options
  • Save bhaskarmelkani/69929f313bb3d66a34425c40743fcfb7 to your computer and use it in GitHub Desktop.
Save bhaskarmelkani/69929f313bb3d66a34425c40743fcfb7 to your computer and use it in GitHub Desktop.
Script for validating Debit/Credit card number using Luhn's Theorem.
function luhn_validate(n){
var l = /^[0-9]+$/.test(n)&&n.length, s = 0, b = 1;
while (l)
s += (b ^= 1) ? +[0, 2, 4, 6, 8, 1, 3, 5, 7, 9][+n[--l]] : +n[--l];
return !!s && s % 10 === 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment