For those who work with handling credit card information you have either or soon will need to carry out a luhn check, below is a useful JavaScript luhn check;
< script type = “text/javascript” >//<![CDATA[
function checkCC(s) {
var i, n, c, r, t;
r = "";
for (i = 0; i < s.length; i++) {
c = parseInt(s.charAt(i), 10);
if (c >= 0 && c <= 9) r = c + r;
}
if (r.length <= 1) return false;
t = "";
for (i = 0; i < r.length; i++) {
c = parseInt(r.charAt(i), 10);
if (i % 2 != 0) c *= 2;
t = t + c;
}
n = 0;
for (i = 0; i < t.length; i++) {
c = parseInt(t.charAt(i), 10);
n = n + c;
}
if (n != 0 && n % 10 == 0) return true;
else return false;
}
function validateForm(f) {
var s = f.value;
if (checkCC(s)) alert("OK");
else alert("Please check your card number");
return false;
}//]]></script>
I haven’t added in checking the first number/s validation, as I have found there to be an inconsistency as to what number/s are used on what card type (an example was that I was told by one bank that a Maestro card couldn’t start with 67, only Solo, but I have a Maestro card that does!)
I’ll be posting a vb.net and c# version soon, as I still can’t believe how many of you Dev’s rely completely on client side JS as your only validation method.