Create USPS PostNet String With ColdFusion

I know PostNet is going to expire in a couple of months, but today I needed to create a few shipping labels, So I put to-gether a simple function to assemble a PostNet string. Mind you, this does not create the actual PostNet barcode. Insted, I use a PostNet font to create the barcode. What I put together here creates a valid PostNet string from a ZIP code so that you can print it with a PostNet barcode font. Pass your zip5 or zip9 to the function, and it will return the string to be printed.

   1: <cffunction name="makepostnet" returntype="string" output="no">
   2:  <cfargument name="zipcode" required="no" type="string" default="">
   3:  <cfset local.postnet = trim(replace(arguments.zipcode,"-",""))>
   4:  <cfif len(local.postnet)>
   5:  <cfset local.sum = 0>
   6:  <cfloop from="1" to="#len(local.postnet)#" index="local.c">
   7:     <cfset local.sum = local.sum + val(mid(local.postnet,local.c,1))>
   8:  </cfloop>
   9:  <cfset local.lastdigit = mid(local.sum,len(local.sum),1)>
  10:  <cfif val(local.lastdigit)>
  11:     <cfset local.sumcheck = 10-local.lastdigit>
  12:  <cfelse>
  13:     <cfset local.sumcheck = 0>
  14:  </cfif>
  15:  <cfset local.postnet = "#local.postnet##local.sumcheck#">
  16:  </cfif>
  17:  <cfreturn local.postnet>
  18: </cffunction>
Show/Hide Line Numbers . Full Screen . Plain

2 Comments :
Olivier
Thursday 02 August 2012 11:35 AM
sa ne marche pas le code


<cffunction name= "makepostnet" returntype = "string" output = "no" >
<cfargument name= "zipcode" required="no" type="string" default="" >
<CFSET local.postnet = garniture ( remplacer ( arguments.zipcode, "-" , "" ) ) >
<cfif len ( local.postnet ) >
<CFSET local.sum = 0 >
<cfloop from="1" to="# len (local.postnet) #" index="local.c" >
<CFSET local.sum = local.sum + val ( mi ( local.postnet, local.c, 1 ) ) >
</cfloop>
<CFSET local.lastdigit = mi ( local.sum, len ( local.sum ) , 1 ) >
<cfif val ( local.lastdigit ) >
<CFSET local.sumcheck = 10-local.lastdigit >
<cfelse>
<CFSET local.sumcheck = 0 >
</cfif>
<CFSET local.postnet = "#local.postnet# #local.sumcheck#" >
</cfif>
local.postnet> <cfreturn
</cffunction>
Thursday 02 August 2012 06:03 PM
Seems the code you copy/paste missing some parts