Quick Search:
CODE
Oracle PL/SQL Code Library
JOBS
Find Or Post Oracle Jobs
FORUM
Oracle Discussion & Chat
 PHP Code: Validate credit card numbers Jump to:  
Category: >> PHP Code >> Validate credit card numbers Bookmark and Share

<< lastnext >>

Snippet Name: Validate credit card numbers

Description: A set of functions to check the validity of a credit card number

($number, $type) returns 1 if the account number is valid, zero if not.

The $type argument is optional. If included, it will perform additional checking to
ensure the card is of the type specified.

Valid types include: VISA, Mastercard, Discover, and AMEX.

Comment: (none)

Language: PHP
Highlight Mode: PHP
Last Modified: March 01st, 2009

<?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); 
} 
 
?>
 


Free
Oracle Magazine
Subscriptions
and Oracle White Papers


SQL University.net courses meet the most demanding needs of the business world for advanced education in a cost-effective manner. SQL University.net courses are available immediately for IT professionals and can be taken without disruption of your workplace schedule or processes.

Compared to traditional travel-based training, SQL University.net saves time and valuable corporate resources, allowing companies to do more with less. That's our mission, and that's what we deliver.

Click here to find out more
 
Home      :      Code Library      :      Sponsors      :      Privacy      :      Terms of Use      :      Contact Us 234 users online    © 2009 psoug.org

PSOUG LOGIN
Username: 
Password: 
Forgot your password?