If you build anything that handles product data, like an inventory app, a store import or a label printer, you will get barcode numbers from spreadsheets. Some of them will be broken, and nothing will tell you.

This post covers two things:

  1. How the GS1 check digit works (EAN-13, UPC-A, EAN-8, GTIN-14) and a short JavaScript function to validate it.
  2. Five ways spreadsheets corrupt barcode numbers, and how to catch each one before you print 500 wrong labels.

All code is plain JavaScript with no dependencies.

How the check digit works

The last digit of every EAN or UPC code is a check digit, calculated from the other digits. Scanners use it to catch misreads. You can use it to catch bad data.

The rule is the same for all GS1 codes, as long as you count from the right:

  1. Take every digit except the last one.
  2. Starting from the rightmost of those, multiply digits by 3, 1, 3, 1, ... in turn.
  3. Add the results.
  4. The check digit is whatever brings the total up to the next multiple of 10.

Example: 400638133393 + check digit.

Digit (right to left) 3 9 3 3 3 1 8 3 6 0 0 4
Weight 3 1 3 1 3 1 3 1 3 1 3 1
Product 9 9 9 3 9 1 24 3 18 0 0 4

The sum is 89. The next multiple of 10 is 90, so the check digit is 1, and the full EAN-13 is 4006381333931.

Counting from the right is the trick that makes one function work for every length. In an EAN-13, the weights from the left are 1, 3, 1, 3, .... In a UPC-A they are 3, 1, 3, 1, .... Both are 3, 1, 3, 1, ... from the right.

function gs1CheckDigit(body) { let sum = 0; for (let i = 0; i < body.length; i++) { const d = body.charCodeAt(body.length - 1 - i) - 48; // '0' is char code 48 sum += i % 2 === 0 ? d * 3 : d; } return (10 - (sum % 10)) % 10; } function isValidGtin(code) { if (!/^\d+$/.test(code) || ![8, 12, 13, 14].includes(code.length)) return false; return gs1CheckDigit(code.slice(0, -1)) === Number(code.at(-1)); } gs1CheckDigit('400638133393'); // 1 isValidGtin('4006381333931'); // true (EAN-13) isValidGtin('036000291452'); // true (UPC-A) isValidGtin('4006381333932'); // false 

Always keep barcodes as strings, never numbers. The next section shows why.

A valid check digit only proves the number is well-formed. It does not prove the number is registered to the brand. Real retail barcodes come from GS1, so a made-up number with a correct check digit can still be rejected by a marketplace or a supermarket.

Five ways spreadsheets corrupt barcodes

1. Scientific notation: 8.90123E+12

Excel stores a 13-digit code as a number and may show it as 8.90123E+12. If the file is saved as CSV, that display text is often what ends up in the file.

This one can't be fixed in code. The last digits are gone, and 8.90123E+12 could be thousands of different barcodes. The only safe action is to reject the row and ask for the column to be re-exported as text.

/^\d+(\.\d+)?e\+?\d+$/i.test('8.90123E+12'); // true -> reject the row 

2. Lost leading zeros: 36000291452

UPC-A codes often start with 0. Stored as a number, 036000291452 becomes 36000291452, which is 11 digits.

You can detect it: if an 11-digit value becomes a valid UPC-A when you add a 0 in front, it probably lost its zero. But flag it and don't fix it silently. About 1 in 10 random 11-digit numbers will pass that check by chance.

3. Precision loss above 15 digits

Excel keeps only 15 significant digits. That's fine for EAN-13 and GTIN-14. But an 18-digit SSCC shipping code is silently rounded:

Typed: 376123450000000017 Stored: 376123450000000000 

JavaScript numbers have the same kind of limit (about 15–16 digits), so Number('376123450000000017') gives the same wrong value. Another reason to keep everything as strings.

A heuristic: an 18-digit value ending in several zeros, with a check digit that fails, is very likely a rounded SSCC.

4. A trailing .0: 4006381333931.0

This usually comes from a script, not from Excel. In pandas, a column of codes with one empty cell becomes a float column. Export it and every code gets .0 on the end.

This one is safe to fix: strip a trailing .0 and validate what's left.

5. Thousands separators and invisible spaces

If the cell was formatted as a number with separators, you get 4,006,381,333,931. If someone copied codes from a web page or a PDF, you can get non-breaking spaces (\u00a0) or zero-width spaces (\u200b) that you can't see.

Strip whitespace, including the invisible kinds. Treat commas as a warning, because they mean the column was a number and may also have lost zeros or digits.

Putting it together

function inspectCode(raw) { const s = String(raw).replace(/[\s\u00a0\u200b]/g, ''); if (/^\d+(\.\d+)?e\+?\d+$/i.test(s)) return { ok: false, problem: 'Scientific notation: digits are lost. Re-export the column as text.' }; if (/^\d{1,3}(,\d{3})+$/.test(s)) return { ok: false, problem: 'Thousands separators: the cell was formatted as a number.' }; const v = s.replace(/\.0+$/, ''); if (isValidGtin(v)) return { ok: true, value: v, note: v !== s ? 'Removed a trailing .0' : undefined }; if (/^\d{11}$/.test(v) && isValidGtin('0' + v)) return { ok: false, suggestion: '0' + v, problem: 'Looks like a UPC-A that lost its leading zero.' }; if (/^\d{12}$/.test(v)) return { ok: false, problem: `Check digit fails. As an EAN-13 body, the check digit would be ${gs1CheckDigit(v)}.` }; return { ok: false, problem: 'Not a valid EAN/UPC/GTIN.' }; } 

Running it on some typical spreadsheet values:

Input Result
8.90123E+12 rejected, digits lost
36000291452 flagged, suggests 036000291452
4006381333931.0 ok, 4006381333931
4,006,381,333,931 rejected, number formatting
" 4006381333931\u00a0" ok, 4006381333931
400638133393 flagged, check digit would be 1
4006381333932 rejected, wrong check digit

Better: read the .xlsx directly

All of this checking is damage control. If you can, accept the .xlsx file itself instead of a CSV. With SheetJS and cellText: true, you can read the formatted text of each cell, which avoids many of these problems.

You can do the parsing in a Web Worker in the browser, so the file never has to be uploaded anywhere.

Where this came from

I ran into every one of these while building the bulk label tool on ownbarcode, a free barcode and label generator that runs entirely in the browser. If you just need to check a few codes by hand, there's a check digit calculator there too.

If you've seen a spreadsheet break barcodes in a way that isn't on this list, please share it in the comments. I'm collecting them.