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);
}
Showing posts with label validation. Show all posts
Showing posts with label validation. Show all posts
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:
Email Validation via JavaScript RegEx
I cannot take credit for the RegEx that makes this function work... I got it from the technical cheat sheet gurus at VisiBone (www.visibone.com/javascript). So without further adue:
function validateEmail(email) {
//alphanum,RFC822 chars,@-sign,alphanumbs dashes & dots,2-4 letter suffix,no more no less,case insensitive
return /^[a-z0-9][^\(\)\<\>\@\,\;\:\\\"\[\]]*\@[a-z0-9][a-z0-9\-\.]*\.[a-z]{2,4}$/i.test(email);
}
function validateEmail(email) {
//alphanum,RFC822 chars,@-sign,alphanumbs dashes & dots,2-4 letter suffix,no more no less,case insensitive
return /^[a-z0-9][^\(\)\<\>\@\,\;\:\\\"\[\]]*\@[a-z0-9][a-z0-9\-\.]*\.[a-z]{2,4}$/i.test(email);
}
Subscribe to:
Posts (Atom)