EAN/UCC-13 check digit calculation

Hi there

I’m creating a 2d gs1 data matrix barcode that requires a EAN/UCC-13 check digit calculation on a portion of the barcode string. Could anyone help with the Javascript for calculation of the check digit?

Thanks in advance

marrd

Looks like this might do the trick for you.

function eanCheckDigit(s){
var result = 0;
for (counter = s.length-1; counter >=0; counter–){
result = result + parseInt(s.charAt(counter)) * (1+(2*(counter % 2)));
}
return (10 - (result % 10)) % 10;
}

Source: node.js - How to validate a EAN / GTIN barcode in JavaScript - Stack Overflow

This code produces an “NaN” result with my input string below?

“RP123ABC0005400000000160”

Well, unmodified, your string returns an error in the GS1 online check digit calculator too.

From what I can tell (and I’m no expert on barcodes, just going on the documentation here) you should only be calculating the check digit based on the last 13 numeric characters. With your string and the above code, that gets me a check digit of 4. Is that what you’re expecting?

I get a check digit of 2, when using the GS1 online check digit calculator with the last 13 numeric digits “5400000000160”. This is the response:

You’ve entered 13 digits, this corresponds with a GTIN-14 format of the GTIN.

However, I need the EAN/UCC-13 format, which stipulates the last 12 digits. When using the GS1 online check digit calculator with the last 12 numeric digits “400000000160”, I get a check digit of 7. This is the response:

You’ve entered 12 digits, this corresponds with the GTIN-13 format. It could also be a GLN or the first 13 digits of a GRAI, GDTI or GCN.

I’m a bit confused. The code used above gives me a check digit of 7, which seems to be correct according to the the online calculator.

How did you get a check digit of 4?

I honestly don’t know how I got 4 before. I’m getting 2 today like you.