These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Barcode Check Digit Calculator.

clibor
Registered: Aug 30 2010
Posts: 17
Answered

Hello all,
 
I'm trying to find some script that would enable me to add a check digit calculator to my form. More specifically a GTIN+14 barcode and a UPC-A barcode.
 
I've found alot of online calculators but I'm not quite sure how to extract a javascript from there then incorporate that into my form.
 
Any help or lead in the right direction will be greatly appreciated.
 
Thanks,
 
CLIFF

My Product Information:
Acrobat Pro 9.2, Macintosh
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
This should get you started:

var sVal = "2468135791234"; // Sample value
var sum = 0;
var factor = 3;

for (var i = sVal.length - 1; i > -1; i--) {
sum += +sVal[i] * factor;
factor = 4 - factor;
}

var check_digit = (sum % 10) ? 10 - (sum % 10) : 0;

app.alert(check_digit);
clibor
Registered: Aug 30 2010
Posts: 17
How would I add this to a button.

End user enters 13 digits. hits the button. correct check digit appears in a seperate box beside the entered #

Sorry. I understand how the coding works I'm just unclear on the structuring of the script.
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
Accepted Answer
The script should first get the value of the input field, as a string. Then check to see if the number of digits is correct. Then calculate the check digit using code like that previously posted. Then set the value of the check digit field equal to the check digit. Something like:

// Get input field value as string
var sVal = getField("input_field").valueAsString;
var check_digit = "";

// Calculate check digit
if (sVal.length === 13) {

var sum = 0;
var factor = 3;

for (var i = sVal.length - 1; i > -1; i--) {
sum += +sVal[i] * factor;
factor = 4 - factor;
}

check_digit = (sum % 10) ? 10 - (sum % 10) : 0;

} else {

app.alert("Please enter 13 digits!", 1);

}

// Set the check digit field
getField("check_digit").value = check_digit;


My nice formatting will probably get mangled by the forum software, but you should get the idea. Replace the field names in the code above to suit your form.
clibor
Registered: Aug 30 2010
Posts: 17
Perfect!

Thanks George!

Always good to know there are talented people out there who are willing help us inexperienced ones.

CLIFF