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);
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson