Skip to content

Instantly share code, notes, and snippets.

@twolfson
Forked from OiNutter/LICENSE.txt
Created November 2, 2011 04:14
Show Gist options
  • Save twolfson/1332837 to your computer and use it in GitHub Desktop.
Save twolfson/1332837 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/

// Decreased from 139 to 124 chars =) // Also, now supports string coercion

function validCard(a,b,c,d){a+='';c=0;for(b=a.length;b--;){d=b%2?a[b]:a[b]*2;if(d>9)d=0|d/10+d%10;c+=+d}return c===c&&!c%10}

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

// When testing, doesn't seem to work for any of my cards but neither did the algorithm I forked from. Oh well =/

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+=''; //splits and reverses the credit card number
c=0;
//loops through each digit in credit card number
for(b=a.length;b--;){
d=b%2?a[b]:a[b]*2; // doubles every second number from right thanks to reversing of number
//if double digits, split and add digits together
if(d>9)
d=0|d/10+d%10;
c+=+d //total up
}
return !c%10 //return valid if divisible by 10
}
function validCard(a,b,c,d){a+='';c=0;for(b=a.length;b--;){d=b%2?a[b]:a[b]*2;if(d>9)d=0|d/10+d%10;c+=+d}return c===c&&!c%10}
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
Author

twolfson commented Nov 2, 2011

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