# Script to calculate upc-a check digit

I have this number “01580253300” in a text file (line1, position1). Is there a way to use a script to calculate the check digit ?

Hi @nabel1510,

Can you let me know please why you would like to calculate the check digit? Would you like to do that because you would like to add a UPC-A barcode element to a Template, for example? Please note that, when this is the case, you can add a UPC-A barcode element (with check digit included) to a Template by executing the following steps:

1. Open a existing Template or create a new Template via the Connect Designer application.
2. View the Master Page or Print Context Section to which you would like to add the UPC-A barcode element in Design mode.
3. Go to Insert > Barcode > UPC-A to insert a UPC-A barcode element.
4. Click with the right mouse button on the UPC-A barcode element and select the Barcode… option.
5. Check the Human Readable Message option in case you would like to check the value assigned to the UPC-A barcode element.

Hi Marten

I need to add the human readable upc to the text document. I don’t want the barcode lines, only the numbers. I want to do it with workflow instead of designer because I want it to be faster

Hi nabel,

Something I slapped together to get you started. (VBScript) There is probably a more elegant way.

``````Dim num
Dim even
Dim odd
Dim result
Dim check

num = "69277198116" '"63938200039"

For i = 1 To Len(num)
If i Mod 2 = 0 Then
even = CInt(even) + CInt(Mid(num, i, 1))
Else
odd = CInt(odd) + CInt(Mid(num, i, 1))
End If
Next

'Multiply odd by three
odd = odd * 3

result = even + odd

'Whatever single digit number makes the total a multiple of 10 is the check digit.
check = CInt(Ceil(result / 10) * 10 - result)

watch.log check,2

Function Ceil(ByVal p_Number)
Ceil = 0 - INT(0 - p_Number)
End Function``````

Regards,
S

Thks Sharne, I will try this.

Sharne
I have tried but the check digit is not created. I have this upc in my file 01580253300. I would like this to output to variable, job info %1

The following script stores the check value in JobInfo 1:

``````Watch.SetJobInfo( 1, addUPCCheckDigit("01234567890") );

var check = input.split("").reduce(function(t,c,i){
c = parseInt(c);
return t += (i%2===0) ? c*3 : c;
},0);
return (Math.ceil(check/10) * 10)-check;
}
``````

Of course, change the 01234567890 value to the actual value for which you want the check digit.

Phil
I get this error:
W3602 : Error 0 on line 1, column 55: Microsoft VBScript compilation error: Cannot use parentheses when calling a Sub

Change the language to Enhanced JScript.

wow, it works. thks Phil

It works but my upc is the name of my file ex: 01580253300.xml. What do I need to change in the script ?

You can use something like this:

``````var myUPCNumber = Watch.ExpandString("%o").split(".")[0];
``````

That line grabs the value of %o (which is the original filename, eg 01580253300.xml ), then splits it at the dot into two parts (eg [`"01580253300"`, `"xml"`] , and then assigns the first element of that split to the myUPCNumber variable.

thks Phil I will try this

Phil, sorry I am not good with scripts. Where do I put var myUPCNumber = Watch.ExpandString("%o").split(".")[0]; and where do I say to put the upc + check digit in Variable %1 ?

Here’s the entire script that uses the file name as the UPC number and stores the check digit in JobInfo 1:

``````var myUPCNumber = Watch.ExpandString("%o").split(".")[0];

var check = input.split("").reduce(function(t,c,i){
c = parseInt(c);
return t += (i%2===0) ? c*3 : c;
},0);
return (Math.ceil(check/10) * 10)-check;
}``````
1 Like

thks again Phil…

Why not using `%O` (filename without extension)?

``````var myUPCNumber = Watch.ExpandString("%O");

var check = input.split("").reduce(function(t,c,i){
c = parseInt(c);
return t += (i%2===0) ? c*3 : c;
},0);
return (Math.ceil(check/10) * 10)-check;
}
``````
1 Like

@thomasweber: you’re absolutely right! %O completely slipped my mind. Proof that you should always have a second pair of eyes looking at whatever solution you are implementing.

Thanks!

1 Like

thks Thomasweber…