# Credit Card Algorithm

Everything has an algorithm except something that we do not know about it!
I knew Credit Card Algorithm from 2000, and I found out it from a Pascal program which checks the credit cards.
I googled for this topic in 2008 and found a lot of articles about it! So, there isn’t any secret now!
My proofs are:
http://www.beachnet.com/~hstiles/cardtype.html

I know that no one can use the credit card generator nowadays because of the credit card online validation checking.
So, I explain it more:
If you have a credit card number like this (16 or 15):
ABCD-EFGH-IJKL-MNOP
Or in array:
CCCC-CCCC-CCCC-CCCC
Now the formula is:
(If there is a number more than 9, then subtract 9)
+
(Even array numbers value)
= Result
And, Result MOD 10 must be 0
For Example we want to know whether the below number is a valid credit card number or not:
CC= 4321-9871-2345-3328
The formula is:
(4*2) + 3 + (2*2) + 1 + (9*2-9) + 8 + (7*2-9) + 1 + (2*2) + 3 + (4*2) + 5 + (3*2) + 3 + (2*2) + 8
=
8 + 3 + 4 + 1 + 9 + 8 + 5 + 1 + 4 + 3 + 8 + 5 + 6 + 3 + 4 + 8 = 80
80 Mod 10 = 0
So this is a valid credit card number!
What about the 4444-3333-2222-1111? Check it, it is a valid credit card number!
I found two good JavaScript codes from:
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_22031163.html

Which are:

By Zvonko:

<script>
function checkCardNum(theField){
var theNum = theField.value;
var check = 0;
for(var i=0;i<theNum.length;i++){
var num = theNum.substr(i,1);
num *= ((i+theNum.length)%2)?1:2;
check += (num>9)?num%10+1:num;
}
if(check%10){
theField.select();
theField.focus();
return false;
}
return true;
}
</script>

And another one
By Rama NUTI (rama_krishna580):

<script>
function isValidCreditCard(type, ccnum) {
if (type == “Visa”) {
// Visa: length 16, prefix 4, dashes optional.
var re = /^4\d{3}-?\d{4}-?\d{4}-?\d{4}\$/;
} else if (type == “MC”) {
// Mastercard: length 16, prefix 51-55, dashes optional.
var re = /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}\$/;
} else if (type == “Disc”) {
// Discover: length 16, prefix 6011, dashes optional.
var re = /^6011-?\d{4}-?\d{4}-?\d{4}\$/;
} else if (type == “AmEx”) {
// American Express: length 15, prefix 34 or 37.
var re = /^3[4,7]\d{13}\$/;
} else if (type == “Diners”) {
// Diners: length 14, prefix 30, 36, or 38.
var re = /^3[0,6,8]\d{12}\$/;
}
if (!re.test(ccnum)) return false;
// Remove all dashes for the checksum checks to eliminate negative numbers
ccnum = ccnum.split(“-“).join(“”);
// Checksum (“Mod 10”)
// Add even digits in even length strings or odd digits in odd length strings.
var checksum = 0;
for (var i=(2-(ccnum.length % 2)); i<=ccnum.length; i+=2) {
checksum += parseInt(ccnum.charAt(i-1));
}
// Analyze odd digits in even length strings or even digits in odd length strings.
for (var i=(ccnum.length % 2) + 1; i<ccnum.length; i+=2) {
var digit = parseInt(ccnum.charAt(i-1)) * 2;
if (digit < 10) { checksum += digit; } else { checksum += (digit-9); }
}
if ((checksum % 10) == 0) return true; else return false;
}
</script>

## 2 thoughts on “Credit Card Algorithm”

1. irsdl Post author

I think Iran filtered my site’s IP address automatically without any actual reason! It was blocked from the first time which I have only the welcome page!
Thank you for your comment Iman