Results 1 to 2 of 2

Thread: Validate UPC - Create valid UPC

  1. #1

    Thread Starter
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Validate UPC - Create valid UPC

    This class i created will validate a supplied UPC code.

    Class
    PHP Code:
    <?php
    class upcvalidator {
        
        function 
    createCheckDigit($code) {
            if (
    $code) {
                for (
    $counter=0;$counter<=strlen($code)-1;$counter++) {
                    
    $codearr[]=substr($code,$counter,1);
                }
                
                for (
    $counter=0;$counter<=count($codearr)-1;$counter++) {
                    if ( 
    $counter&) {
                        
    $evensum $evensum $codearr[$counter];
                    } else {
                        
    $oddsum $oddsum $codearr[$counter];
                    }
                }
                
                
    $oddsum $oddsum *3;
                
    $oddeven $oddsum $evensum;
                
                for (
    $number=0;$number<=9;$number++) {
                    if ((
    $oddeven+$number)%10==0) {
                        
    $checksum $number;
                    }
                }
                
                return 
    $checksum;
            } else {
                return 
    false;
            }
        }
        
        function 
    validateUPC($upc) {
            if (
    $upc!="") {
                
    $checkdigit substr($upc, -1);
                
    $code substr($upc0, -1);
                
                
    $checksum $this->createCheckDigit($code);
                if (
    $checkdigit == $checksum) {
                    return 
    true;
                } else {
                    return 
    false;
                }
            } else {
                return 
    false;
            }
        }
        
        function 
    createUPC($code) {
            if (
    $code) {
                
    $checkdigit $this->createCheckDigit($code);
                
    $upc $code $checkdigit;
                
                return 
    $upc;
            } else {
                return 
    false;
            }
        }
    }
    ?>
    Functions

    createCheckDigit($code) - When supplied with an 11 digit code, will return the check digit. No practical use, used for the class.

    createUPC($code) - When supplied with an 11 digit code, will return with the valid check digit amended to the code.

    validateUPC($upc) - When supplied with the 12 digit UPC, will validate the UPC code.

    Example

    Create Class
    PHP Code:
    require_once ("$class.upcvalidator.php");
    $upcvalidate = new upcvalidator
    validateUPC()
    PHP Code:
    //valid upc
    $upc 639382000393;
    $validate $upcvalidate->validateUPC($upc); //returns true

    //invalid upc
    $upc 123456789123;
    $validate $upcvalidate->validateUPC($upc); //returns false 
    createUPC()
    PHP Code:
    $code 12345678900// 11 digit code
    $upc $upcvalidate->createUPC($code);
    echo 
    $upc//returns 123456789005
    //Notice the 5. 5 is the valid check digit. 

  2. #2

    Thread Starter
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: Validate UPC - Create valid UPC

    Here are the attachments
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width