//////////////////////////////////////// //// // Creditcardvalidationroutine /// May15, 2000 /// Byariso // validateCardCode ($ number [, $ cardtype]) //// //
//////////////////////////////////////// ////////////
////
// Credit card validation routine //
// May 15,200 0 //
// By ariso //
// ValidateCardCode ($ number [, $ cardtype]) //
//////////////////////////////////////// ////////////
Function validateCardCode ($ cardnumber, $ cardtype = unknown)
{
// Clean up input
$ Cardtype = strtolower ($ cardtype );
$ Cardnumber = ereg_replace ([-[: space:], $ cardnumber );
// Do type specific checks
If ($ cardtype = unknown ){
// Skip type specific checks
}
Elseif ($ cardtype = mastercard ){
If (strlen ($ cardnumber )! = 16 |! Ereg (5 [1-5], $ cardnumber) return 0;
}
Elseif ($ cardtype = visa ){
If (strlen ($ cardnumber )! = 13 & strlen ($ cardnumber )! = 16) | substr ($ cardnumber, 0, 1 )! = 4)
Return 0;
}
Elseif ($ cardtype = amex ){
If (strlen ($ cardnumber )! = 15 |! Ereg (3 [47], $ cardnumber) return;
}
Elseif ($ cardtype = discover ){
If (strlen ($ cardnumber )! = 16 | substr ($ cardnumber, 0, 4 )! = 6011) return 0;
}
Else {
// Invalid type entered
Return-1;
}
// Start MOD 10 checks
$ Dig = toCharArray ($ cardnumber );
$ Numdig = sizeof ($ dig );
$ IntIntJ = 0;
For ($ intI = ($ numdig-2); $ intI> = 0; $ intI-= 2 ){
$ Dbl [$ intIntJ] = $ dig [$ intI] * 2;
$ IntIntJ ++;
}
$ Dblsz = sizeof ($ dbl );
$ Validate = 0;
For ($ intI = 0; $ intI <$ dblsz; $ intI ++ ){
$ Add = toCharArray ($ dbl [$ intI]);
For ($ intIntJ = 0; $ intIntJ $ Validate + = $ add [$ intIntJ];
}
$ Add =;
}
For ($ intI = ($ numdig-1); $ intI> = 0; $ intI-= 2 ){
$ Validate + = $ dig [$ intI];
}
If (substr ($ validate,-1, 1) = 0) return 1;
Else return 0;
}
// Takes a string and returns an array of characters
Function toCharArray ($ intInput ){
$ Len = strlen ($ intInput );
For ($ intIntJ = 0; $ intIntJ <$ len; $ intIntJ ++ ){
$ Char [$ intIntJ] = substr ($ intInput, $ intIntJ, 1 );
}
Return ($ char );
}
?>