saghir69
11-24-2008, 03:13 PM
Client Side Credit Card Validation
I am just writing a form validation code for a credit card processing form. The form will be submitted to Paypal and obviously the numbers will be checked by paypal and rejected if incorrect details are sent.
However I have come across client side CC number validation scripts.
Do you lot use this validation? Is there a negative side to using such validation?
Does anyone have a script for this they can recommend?
This the one I am currently thinking of using.
PHP Code:
function Mod10(ccNumb) {テつ*// v2.0
var valid = "0123456789"テつ*テつ*// Valid digits in a credit card number
var len = ccNumb.length;テつ*テつ*// The length of the submitted cc number
var iCCN = parseInt(ccNumb);テつ*テつ*// integer of ccNumb
var sCCN = ccNumb.toString();テつ*テつ*// string of ccNumb
sCCN = sCCN.replace (/^s+|s+$/g,'');テつ*テつ*// strip spaces
var iTotal = 0;テつ*テつ*// integer total set at zero
var bNum = true;テつ*テつ*// by default assume it is a number
var bResult = false;テつ*テつ*// by default assume it is NOT a valid cc
var temp;テつ*テつ*// temp variable for parsing string
var calc;テつ*テつ*// used for calculation of each digit
// Determine if the ccNumb is in fact all numbers
for (var j=0; j<len; j++) {
テつ*テつ*temp = "" + sCCN.substring(j, j+1);
テつ*テつ*if (valid.indexOf(temp) == "-1"){bNum = false;}
}
// if it is NOT a number, you can either alert to the fact, or just pass a failure
if(!bNum){
テつ*テつ*/*alert("Not a Number");*/bResult = false;
}
// Determine if it is the proper length
if((len == 0)
} else{テつ*テつ*// ccNumb is a number and the proper length - let's see if it is a valid card number
テつ*テつ*if(len >= 15){テつ*テつ*// 15 or 16 for Amex or V/MC
テつ*テつ*テつ*テつ*for(var i=len;i>0;i--){テつ*テつ*// LOOP throught the digits of the card
テつ*テつ*テつ*テつ*テつ*テつ*calc = parseInt(iCCN) % 10;テつ*テつ*// right most digit
テつ*テつ*テつ*テつ*テつ*テつ*calc = parseInt(calc);テつ*テつ*// assure it is an integer
テつ*テつ*テつ*テつ*テつ*テつ*iTotal += calc;テつ*テつ*// running total of the card number as we loop - Do Nothing to first digit
テつ*テつ*テつ*テつ*テつ*テつ*i--;テつ*テつ*// decrement the count - move to the next digit in the card
テつ*テつ*テつ*テつ*テつ*テつ*iCCN = iCCN / 10;テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*// subtracts right most digit from ccNumb
テつ*テつ*テつ*テつ*テつ*テつ*calc = parseInt(iCCN) % 10 ;テつ*テつ*テつ*テつ*// NEXT right most digit
テつ*テつ*テつ*テつ*テつ*テつ*calc = calc *2;テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*// multiply the digit by two
テつ*テつ*テつ*テつ*テつ*テつ*// Instead of some screwy method of converting 16 to a string and then parsing 1 and 6 and then adding them to make 7,
テつ*テつ*テつ*テつ*テつ*テつ*// I use a simple switch statement to change the value of calc2 to 7 if 16 is the multiple.
テつ*テつ*テつ*テつ*テつ*テつ*switch(calc){
テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*case 10: calc = 1; break;テつ*テつ*テつ*テつ*テつ*テつ*テつ*//5*2=10 break;テつ*テつ*テつ*テつ*テつ*テつ*テつ*//6*2=12 break;テつ*テつ*テつ*テつ*テつ*テつ*テつ*//7*2=14 break;テつ*テつ*テつ*テつ*テつ*テつ*テつ*//8*2=16 break;テつ*テつ*テつ*テつ*テつ*テつ*テつ*//9*2=18 テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*//4*2= 8 テつ*テつ*// subtracts right most digit from ccNum
テつ*テつ*テつ*テつ*iTotal += calc;テつ*テつ*// running total of the card number as we loop
テつ*テつ*}テつ*テつ*// END OF LOOP
テつ*テつ*if ((iTotal%10)==0){テつ*テつ*// check to see if the sum Mod 10 is zero
テつ*テつ*テつ*テつ*bResult = true;テつ*テつ*// This IS (or could be) a valid credit card number.
テつ*テつ*} else {
テつ*テつ*テつ*テつ*bResult = false;テつ*テつ*// This could NOT be a valid credit card number
テつ*テつ*テつ*テつ*}
テつ*テつ*}
}
// change alert to on-page display or other indication as needed.
if(bResult) {
テつ*テつ*alert("This IS a valid Credit Card Number!");
}
if(!bResult){
テつ*テつ*alert("This is NOT a valid Credit Card Number!");
document.forms[0].cc_field.focus();
}
テつ*テつ*return bResult; // Return the results
}
// -->
I am just writing a form validation code for a credit card processing form. The form will be submitted to Paypal and obviously the numbers will be checked by paypal and rejected if incorrect details are sent.
However I have come across client side CC number validation scripts.
Do you lot use this validation? Is there a negative side to using such validation?
Does anyone have a script for this they can recommend?
This the one I am currently thinking of using.
PHP Code:
function Mod10(ccNumb) {テつ*// v2.0
var valid = "0123456789"テつ*テつ*// Valid digits in a credit card number
var len = ccNumb.length;テつ*テつ*// The length of the submitted cc number
var iCCN = parseInt(ccNumb);テつ*テつ*// integer of ccNumb
var sCCN = ccNumb.toString();テつ*テつ*// string of ccNumb
sCCN = sCCN.replace (/^s+|s+$/g,'');テつ*テつ*// strip spaces
var iTotal = 0;テつ*テつ*// integer total set at zero
var bNum = true;テつ*テつ*// by default assume it is a number
var bResult = false;テつ*テつ*// by default assume it is NOT a valid cc
var temp;テつ*テつ*// temp variable for parsing string
var calc;テつ*テつ*// used for calculation of each digit
// Determine if the ccNumb is in fact all numbers
for (var j=0; j<len; j++) {
テつ*テつ*temp = "" + sCCN.substring(j, j+1);
テつ*テつ*if (valid.indexOf(temp) == "-1"){bNum = false;}
}
// if it is NOT a number, you can either alert to the fact, or just pass a failure
if(!bNum){
テつ*テつ*/*alert("Not a Number");*/bResult = false;
}
// Determine if it is the proper length
if((len == 0)
} else{テつ*テつ*// ccNumb is a number and the proper length - let's see if it is a valid card number
テつ*テつ*if(len >= 15){テつ*テつ*// 15 or 16 for Amex or V/MC
テつ*テつ*テつ*テつ*for(var i=len;i>0;i--){テつ*テつ*// LOOP throught the digits of the card
テつ*テつ*テつ*テつ*テつ*テつ*calc = parseInt(iCCN) % 10;テつ*テつ*// right most digit
テつ*テつ*テつ*テつ*テつ*テつ*calc = parseInt(calc);テつ*テつ*// assure it is an integer
テつ*テつ*テつ*テつ*テつ*テつ*iTotal += calc;テつ*テつ*// running total of the card number as we loop - Do Nothing to first digit
テつ*テつ*テつ*テつ*テつ*テつ*i--;テつ*テつ*// decrement the count - move to the next digit in the card
テつ*テつ*テつ*テつ*テつ*テつ*iCCN = iCCN / 10;テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*// subtracts right most digit from ccNumb
テつ*テつ*テつ*テつ*テつ*テつ*calc = parseInt(iCCN) % 10 ;テつ*テつ*テつ*テつ*// NEXT right most digit
テつ*テつ*テつ*テつ*テつ*テつ*calc = calc *2;テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*// multiply the digit by two
テつ*テつ*テつ*テつ*テつ*テつ*// Instead of some screwy method of converting 16 to a string and then parsing 1 and 6 and then adding them to make 7,
テつ*テつ*テつ*テつ*テつ*テつ*// I use a simple switch statement to change the value of calc2 to 7 if 16 is the multiple.
テつ*テつ*テつ*テつ*テつ*テつ*switch(calc){
テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*case 10: calc = 1; break;テつ*テつ*テつ*テつ*テつ*テつ*テつ*//5*2=10 break;テつ*テつ*テつ*テつ*テつ*テつ*テつ*//6*2=12 break;テつ*テつ*テつ*テつ*テつ*テつ*テつ*//7*2=14 break;テつ*テつ*テつ*テつ*テつ*テつ*テつ*//8*2=16 break;テつ*テつ*テつ*テつ*テつ*テつ*テつ*//9*2=18 テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*//4*2= 8 テつ*テつ*// subtracts right most digit from ccNum
テつ*テつ*テつ*テつ*iTotal += calc;テつ*テつ*// running total of the card number as we loop
テつ*テつ*}テつ*テつ*// END OF LOOP
テつ*テつ*if ((iTotal%10)==0){テつ*テつ*// check to see if the sum Mod 10 is zero
テつ*テつ*テつ*テつ*bResult = true;テつ*テつ*// This IS (or could be) a valid credit card number.
テつ*テつ*} else {
テつ*テつ*テつ*テつ*bResult = false;テつ*テつ*// This could NOT be a valid credit card number
テつ*テつ*テつ*テつ*}
テつ*テつ*}
}
// change alert to on-page display or other indication as needed.
if(bResult) {
テつ*テつ*alert("This IS a valid Credit Card Number!");
}
if(!bResult){
テつ*テつ*alert("This is NOT a valid Credit Card Number!");
document.forms[0].cc_field.focus();
}
テつ*テつ*return bResult; // Return the results
}
// -->
