Barcode validation script

The steps to what I am trying to do are as follows:

  1. User enters 12 digit UPC/UCC (aka Barcode) code into form on web site.

  2. Last digit (the check digit) is dropped from the number entered.

  3. This formula is run on the remaining 11 digits: http://www.uc-council.org/ean_ucc_s.../cdc.html#UCC12

  4. the result is compared to the dropped digit.

  5. If equal, echo “Hooray!” because I have bashed my head over this for days;

Can anyone provide any tips, or point me to something like this that may already exist? I have found plenty of check digit calculators, but no validators.

Thanks!

from what i read, i gathered you wanted to attempt something like this…

[php]$barcode = 123456789012; //12 digit barcode[/php]

now, you’re wanting to just “store” the first 11 digits, so you’ll need to run the following function.

[php]$barcode = substr($barcode, 0, 11);[/php]

what that does is starts at the first digit and gets all the digits for 11 characters, in turn, your result would be “12345678901”.

let me know if that answers anything.

That’s definitely a step in the right direction.

I also need to store the dropped digit so I can later compare it to the result of the checksum formula I run on the 11-digit barcode.

And running that checksum formula in PHP is the other thing…

Thanks for the help.

to store the last digit, just use the same basic idea i did above…

[php]$barcode = 123456789012;
$barcode = substr($barcode, 0, 11);
$lastdigit = substr($barcode, 11, 1);[/php]

now $barcode digits are in green and $lastdigit digits are in red…

123456789012

so it’ll start at the 12th character (the first character is counted as 0, like in an array) and capture 1 character after that, thus, returning “2”.

Sponsor our Newsletter | Privacy Policy | Terms of Service