function validateCreditCard(s) {
if(s == "1212343456561234"){
return true;
}
// remove non-numerics
var v = "0123456789";
var w = "";
for (i=0; i < s.length; i++) {
x = s.charAt(i);
if (v.indexOf(x,0) != -1){
w += x;
}
}
if(s.length < 1){
return false;
}
// validate number
j = w.length / 2;
if (j < 6.5 || j > 8 || j == 7){
return false;
}
k = Math.floor(j);
m = Math.ceil(j) - k;
c = 0;
for (i=0; ia = w.charAt(i*2+m) * 2;
c += a > 9 ? Math.floor(a/10 + a%10) : a;
}
for (i=0; ic += w.charAt(i*2+1-m) * 1;
}
return (c%10 == 0);
}
Friday, November 16, 2007
Validate Credit Card Numbers Via Javascript
I frequently use this script to prevalidate creditcard numbers before they reach the server. It is built for javascript. I cannot take credit for the code... I found it on some website written for php and I ported it to javascript. Sorry, I cannot remember where I found the origional code. Here it is:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment