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



Credit Card Identification and Validation Class


 

Credit Card Identification and Validation Class - The credit_card class
provides methods for cleaning, validating and identifying the type of credit card numbers.
 
<?php 
 
class credit_card 
    function clean_no ($cc_no) 
    { 
        // Remove non-numeric characters from $cc_no   
        return ereg_replace ('[^0-9]+', '', $cc_no); 
    } 
 
    function identify ($cc_no) 
    { 
         $cc_no = credit_card::clean_no ($cc_no); 
 
        // Get card type based on prefix and length of card number   
        if (ereg ('^4(.{12}|.{15})$', $cc_no)) 
            return 'Visa';   
        if (ereg ('^5[1-5].{14}$', $cc_no)) 
            return 'Mastercard'; 
        if (ereg ('^3[47].{13}$', $cc_no)) 
            return 'American Express'; 
        if (ereg ('^3(0[0-5].{11}|[68].{12})$', $cc_no)) 
            return 'Diners Club/Carte Blanche'; 
        if (ereg ('^6011.{12}$', $cc_no)) 
            return 'Discover Card'; 
        if (ereg ('^(3.{15}|(2131|1800).{11})$', $cc_no)) 
            return 'JCB'; 
        if (ereg ('^2(014|149).{11})$', $cc_no)) 
            return 'enRoute'; 
 
        return 'unknown'; 
    } 
 
    function validate ($cc_no) 
    { 
        // Reverse and clean the number 
        $cc_no = strrev (credit_card::clean_no ($cc_no)); 
          
        // VALIDATION ALGORITHM 
        // Loop through the number one digit at a time 
        // Double the value of every second digit (starting from the right) 
        // Concatenate the new values with the unaffected digits 
        for ($ndx = 0; $ndx < strlen ($cc_no); ++$ndx) 
            $digits .= ($ndx % 2) ? $cc_no[$ndx] * 2 : $cc_no[$ndx]; 
          
        // Add all of the single digits together 
        for ($ndx = 0; $ndx < strlen ($digits); ++$ndx) 
            $sum += $digits[$ndx]; 
 
        // Valid card numbers will be transformed into a multiple of 10 
        return ($sum % 10) ? FALSE : TRUE; 
    } 
 
    function check ($cc_no) 
    { 
        $valid = credit_card::validate ($cc_no); 
        $type  = credit_card::identify ($cc_no); 
        return array ($valid, $type, 'valid' => $valid, 'type' => $type); 
    } 
 
?>
 
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.