CC Validation
by Adam Smith
HTML
<div id="output"></div>
JavaScript
function getCreditCardType(i) {
var e = "unknown";
return /^5[1-5]/.test(i) ? e = "MasterCard" : /^4/.test(i) ? e = "Visa" : /^3[47]/.test(i) ? e = "American Express" : /^(?:2131|1800|35)/.test(i) ? e = "JCB" : /^3(?:0[0-5]|[68])/.test(i) ? e = "Diners Club" : /^6(?:011|5)/.test(i) && (e = "Discover"),
e
}
function getFullType(i) {
var e = "unknown";
return /^5[1-5][0-9]{14}$/.test(i) ? e = "MasterCard" : /^4[0-9]{12}(?:[0-9]{3})?$/.test(i) ? e = "Visa" : /^3[47][0-9]{13}$/.test(i) ? e = "American Express" : /^(?:2131|1800|35\d{3})\d{11}$/.test(i) ? e = "JCB" : /^3(?:0[0-5]|[68][0-9])[0-9]{11}$/.test(i) ? e = "Diners Club" : /^6(?:011|5[0-9]{2})[0-9]{12}$/.test(i) && (e = "Discover"),
e
}
function Calculate(e) {
var a = 0;
for (i = 0; i < e.length; i++)
a += parseInt(e.substring(i, i + 1));
var n = new Array(0,1,2,3,4,-4,-3,-2,-1,0);
for (i = e.length - 1; i >= 0; i -= 2) {
var t = parseInt(e.substring(i, i + 1))
, r = n[t];
a += r
}
var s = a % 10;
return s = 10 - s,
10 == s && (s = 0),
s
}
function Validate(i) {
var e = parseInt(i.substring(i.length - 1, i.length))
, a = i.substring(0, i.length - 1);
return Calculate(a) == parseInt(e) ? !0 : !1
}
$('#output').text(Validate('4111111111111111'));