Skip to content

Instantly share code, notes, and snippets.

@mrpants
Forked from 140bytes/LICENSE.txt
Last active October 11, 2015 23:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrpants/3939122 to your computer and use it in GitHub Desktop.
Save mrpants/3939122 to your computer and use it in GitHub Desktop.
Credit Card Validation
function(
c, // credit card number to validate
a, // length of credit card (placeholder)
r, // sum of digits (for Luhn algorithm) (placeholder)
d, // current digit (placeholder)
x // counter (placeholder)
){
for(
a = c.length, // get the card's length
x = r = 0; // initialize counter and sum
a--; // work through the string backwards
)
d = +c[a], // convert current digit to integer
r += // sum for Luhn algorithm..
x % 2 ? // if the counter is even or 0..
d < 5 ? // if the current digit is less than 5
// (a.k.a. multiplying digit by two will
// result in a single digit product)
d * 2 // add the current digit times 2 to the sum
: // else
d * 2 - 9 // add the current digit times 2 minus 9
: // else
d, // add the current digit to the sum
x++; // increment counter
return!(r%10) // return true if sum % 10 is 0
}
function(c,a,r,d,x){for(a=c.length,x=r=0;a--;)d=+c[a],r+=x%2?d<5?d*2:d*2-9:d,x++;return!(r%10)}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
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": "creditCardValidator",
"description": "A credit card validation function using the Luhn algorithm.",
"keywords": [
"creditcard",
"validation",
"luhn"
]
}
<!DOCTYPE html>
<html>
<head>
<title>140byt.es Credit Card Validation Test</title>
<style type="text/css">
th,td { padding: 5px 10px }
</style>
</head>
<body>
<table border="1">
<thead>
<tr>
<th>CreditCard</th>
<th>Expected</th>
<th>Actual</th>
</tr>
</thead>
<tbody>
<tr>
<td>378282246310005</td>
<td>true</td>
<td id="cc0"></td>
</tr>
<tr>
<td>371449635398431</td>
<td>true</td>
<td id="cc1"></td>
</tr>
<tr>
<td>378734493671000</td>
<td>true</td>
<td id="cc2"></td>
</tr>
<tr>
<td>30569309025904</td>
<td>true</td>
<td id="cc3"></td>
</tr>
<tr>
<td>38520000023237</td>
<td>true</td>
<td id="cc4"></td>
</tr>
<tr>
<td>6011111111111117</td>
<td>true</td>
<td id="cc5"></td>
</tr>
<tr>
<td>6011000990139424</td>
<td>true</td>
<td id="cc6"></td>
</tr>
<tr>
<td>5555555555554444</td>
<td>true</td>
<td id="cc7"></td>
</tr>
<tr>
<td>5105105105105100</td>
<td>true</td>
<td id="cc8"></td>
</tr>
<tr>
<td>4111111111111111</td>
<td>true</td>
<td id="cc9"></td>
</tr>
<tr>
<td>4012888888881881</td>
<td>true</td>
<td id="cc10"></td>
</tr>
<tr>
<td>4222222222222</td>
<td>true</td>
<td id="cc11"></td>
</tr>
<tr>
<td>378282246310006</td>
<td>false</td>
<td id="cc12"></td>
</tr>
<tr>
<td>371449635398439</td>
<td>false</td>
<td id="cc13"></td>
</tr>
<tr>
<td>378734493671005</td>
<td>false</td>
<td id="cc14"></td>
</tr>
<tr>
<td>4111111111111112</td>
<td>false</td>
<td id="cc15"></td>
</tr>
</tbody>
</table>
<script>
var v = function(c,a,r,d,x){for(a=c.length,x=r=0;a--;)d=+c[a],r+=x%2?d<5?d*2:d*2-9:d,x++;return!(r%10)};
var cards = ["378282246310005", // Valid
"371449635398431",
"378734493671000",
"30569309025904",
"38520000023237",
"6011111111111117",
"6011000990139424",
"5555555555554444",
"5105105105105100",
"4111111111111111",
"4012888888881881",
"4222222222222",
"378282246310006", // Invalid
"371449635398439",
"378734493671005",
"4111111111111112"];
for (var i=0; i < cards.length; i++) {
var result = v(cards[i]);
document.getElementById("cc" + i).innerHTML = result;
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment