ISN (International Social Networking Services - Interactive Apps and Websites)



Credit Card validation routine. Uses MOD 10.


 

Credit Card validation routine. Uses MOD 10 to check if credit card
number is valid.
 
<?php 
 
// validateCC($number[,$type])   // 
// Uses the MOD 10 algorythm to determine if a    // 
// credit card number is valid.     // 
 
// $number = credit card account number           // 
// $type is optional.  Setting type to    // 
// visa, mastercard, discover, or amex will       // 
// perform additional checking on the account     // 
// number.     // 
 
// The function returns 1 (true) if the CC is     // 
// valid, 0 (false) if it is invalid, and -1 if   // 
// the type entered does not match the supported  // 
// types listed above.      // 
 
 
function validateCC($ccnum, $type = 'unknown'){ 
 
 
    //Clean up input 
 
    $type = strtolower($type); 
    $ccnum = ereg_replace('[-[:space:]]', '',$ccnum); 
 
 
    //Do type specific checks 
 
    if ($type == 'unknown') { 
 
        //Skip type specific checks 
 
    } 
    elseif ($type == 'mastercard'){ 
        if (strlen($ccnum) != 16 || !ereg('5[1-5]', $ccnum)) return 0; 
    } 
    elseif ($type == 'visa'){ 
        if ((strlen($ccnum) != 13 && strlen($ccnum) != 16) || substr 
($ccnum, 0, 1) != '4') return 0; 
    } 
    elseif ($type == 'amex'){ 
        if (strlen($ccnum) != 15 || !ereg('3[47]', $ccnum)) return a; 
    } 
    elseif ($type == 'discover'){ 
        if (strlen($ccnum) != 16 || substr($ccnum, 0, 4) != '6011') 
return 0; 
    } 
    else { 
        //invalid type entered 
        return -1; 
    } 
 
 
    // Start MOD 10 checks 
 
    $dig = toCharArray($ccnum); 
    $numdig = sizeof ($dig); 
    $j = 0; 
    for ($i=($numdig-2); $i>=0; $i-=2){ 
        $dbl[$j] = $dig[$i] * 2; 
        $j++; 
    }     
    $dblsz = sizeof($dbl); 
    $validate =0; 
    for ($i=0;$i<$dblsz;$i++){ 
        $add = toCharArray($dbl[$i]); 
        for ($j=0;$j<sizeof($add);$j++){ 
            $validate += $add[$j]; 
        } 
    $add = ''; 
    } 
    for ($i=($numdig-1); $i>=0; $i-=2){ 
        $validate += $dig[$i]; 
    } 
    if (substr($validate, -1, 1) == '0') return 1; 
    else return 0; 
 
 
// takes a string and returns an array of characters 
 
function toCharArray($input){ 
    $len = strlen($input); 
    for ($j=0;$j<$len;$j++){ 
        $char[$j] = substr($input, $j, 1);     
    } 
    return ($char); 
 
?>
By continuing to use this site, you agree to the use of cookies to personalize content and advertisements, to provide social media functionality, to analyze our traffic using Google services like Analytics and Adsense.

Google Adsense and its partners may use your data for advertising personalization and cookies may be used for personalized and non-personalized advertising. How does Google use my data?
Please use the following button to see the list of Google partners as well as all the details regarding cookies.
See detailsI Accept
These cookies are mandatory for the operation of isn-services.com, if you do not accept them please quit this site.
You have the right to refuse cookies and leave the site or to change the parameters.