Skip to content

Instantly share code, notes, and snippets.

@OiNutter
Forked from 140bytes/LICENSE.txt
Created May 26, 2011 15:36
Show Gist options
  • Save OiNutter/993370 to your computer and use it in GitHub Desktop.
Save OiNutter/993370 to your computer and use it in GitHub Desktop.
Credit Card Validation similar to the Luhn Algorithm

Validates a credit card number based on the information given at

http://www.mint.com/blog/trends/credit-card-code-01202011/

var validCard=function(a,b,c,d) {a=a.split('').reverse();c=0;for(b in a){d=b%2!=0?a[b]2:a[b];if(d>=10)d=0|d/10+d%10;c+= d1} return c==70}

validCard('No Way I'm Putting Mine Here!!!') // returns true or false

function(
a, //credit card number
b, //placeholder
c, //placeholder
d //placeholder
) {
/*
Validates according to algorithm given here:
http://www.mint.com/blog/trends/credit-card-code-01202011/
*/
a=a.split('').reverse(); //splits and reverses the credit card number
c=0;
//loops through each digit in credit card number
for(b in a){
d=b%2!=0?a[b]*2:a[b]; // doubles every second number from right thanks to reversing of number
//if double digits, split and add digits together
if(d>=10)
d=0|d/10+d%10;
c+=+d //total up
}
return c==70 //return valid if equal to 70
}
function(a,b,c,d) {a=a.split('').reverse();c=0;for(b in a){d=b%2!=0?a[b]*2:a[b];if(d>=10)d=0|d/10+d%10;c+= d*1} return c==70}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Will McKenzie<www.oinutter.co.uk>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "creditCardValidation",
"description": "Validates a credit card.",
"keywords": [
"creditcard",
"validation",
"form",
"checksum"
]
}
@twolfson
Copy link

twolfson commented Nov 2, 2011

https://gist.github.com/1332837
Patched it on my fork ;)

@OiNutter
Copy link
Author

OiNutter commented Nov 2, 2011

tbh I'd given up on this when I saw the luhn10 algorithm had already been done here. But thanks for patching it anyway! :)

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