CFLib.org – Common Function Library Project

UPCCheckDigit(upc)

Last updated June 15, 2002

author

Pete Jacoby

Version: 1 | Requires: CF5 | Library: StrLib

Description:
Given the first 11 digits of a UPC-A barcode, returns the 12th check digit.

Return Values:
Returns a numeric value.

Example:

<cfset barcode=12345678901>
<cfoutput>
Check digit for #barcode# is #UPCCheckDigit(barcode)#
</cfoutput>

Parameters:

Name Description Required
upc 11 digit UPC-A code Yes

Full UDF Source:

/**
 * Calculates a UPC-A check digit.
 * 
 * @param upc      11 digit UPC-A code (Required)
 * @return Returns a numeric value. 
 * @author Pete Jacoby (cf@subnova.net) 
 * @version 1, June 15, 2002 
 */
function UPCCheckDigit(upc){
var odd = 0;
var even = 0;
var total = 0;

if (NOT IsNumeric(upc) OR Len(upc) NEQ 11)
    return(-1);

for (i=1; i LT 12; i=i+1){
        if (i MOD 2)
            odd=odd+Mid(upc, i, 1);
        else
            even=even+Mid(upc, i, 1);
    }

total=(odd*3)+even;
total=total mod 10;

if (total eq 0)
    return 0;
else
    return(10-total);
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson