GTIN/EAN Generator Code
In this blog post, we demonstrate how to generate and validate EAN-13 barcodes using JavaScript. EAN-13, a 13-digit barcode standard, is crucial for tracking products in retail and inventory systems globally. Each EAN-13 barcode includes a GS1 country code, a company code, a product item code, and a single checksum digit for validation.
First, here is a JavaScript function to validate an EAN-13 barcode. This function ensures that the barcode complies with the EAN-13 checksum formula:
function isValidEAN13(ean) {
const eanString = ean.toString();
// Ensure EAN is exactly 13 digits and all are numbers
if (!eanString.match(/^\d{13}$/)) {
return false;
}
let sum = 0;
// Loop through the digits and apply the EAN-13 algorithm
for (let i = 0; i < 12; i++) {
const digit = parseInt(eanString[i], 10);
// Multiply even index digits by 1, odd index digits by 3
sum += i % 2 === 0 ? digit : digit * 3;
}
// Calculate the checksum digit
const checksum = (10 - (sum % 10)) % 10;
// Compare the calculated checksum with the last digit
return checksum === parseInt(eanString[12], 10);
}
// Examples
console.log(isValidEAN13("4006381333931")); // true (valid EAN-13)
console.log(isValidEAN13("4006381333932")); // false (invalid EAN-13)
The initial digits of an EAN-13 barcode represent the GS1 country code. These codes identify the country or economic region where the manufacturer is registered. The full list of GS1 country codes can be found on Wikipedia’s GS1 country codes page and GS1’s website.
The prefix
is followed by a series of digits denoting the company within that country. After the company identifier, the next section includes the product code, assigned by the manufacturer to distinguish between their different products. The final digit of the EAN-13 barcode is the checksum, calculated using the preceding digits. This checksum ensures the integrity of the barcode, providing a method to check for any errors in the scanning or manual entry of the barcode number.
Now let’s create some JavaScript to allow generating a ‘valid’ EAN. This modification enables creating EAN-13 codes starting with predefined country codes:
// Generate an EAN with a certain prefix.
// Find prefixed on:
// https://en.wikipedia.org/wiki/List_of_GS1_country_codes
function generateEAN13WithPrefix(prefix) {
let ean = prefix;
let sum = 0;
// Generate the rest of the digits until the length is 12
for (let i = prefix.length; i < 12; i++) {
// Random digit between 0 and 9
const digit = Math.floor(Math.random() * 10);
ean += digit;
// Alternate weighting
sum += i % 2 === 0 ? digit * 3 : digit;
}
// Calculate the checksum digit
const checksum = (10 - (sum % 10)) % 10;
ean += checksum; // Append the checksum digit
return ean;
}
// Example: Generate an EAN-13 barcode with a US country code (020-029)
console.log(generateEAN13WithPrefix("020")); // Eg. 0203940927136
This enhanced function allows for the generation of EAN-13 barcodes that are specific to a country or region, based on the GS1 prefix provided.