Validate your EAN barcode


On one of the applications I am working on, I had to validate an EAN (European Article Number) barcode.
This application is mostly using JavaScript validation so I asked my friend Google to find me a JavaScript method which would check my EAN barcode.
It found validators in different languages but none in JavaScript. 🙁

Because one is never better served than by oneself, I decided to write it myself and share it with you. 😉

function checkEan(eanCode) {
	// Check if only digits
	var ValidChars = "0123456789";
	for (i = 0; i < eanCode.length; i++) { 
		digit = eanCode.charAt(i); 
		if (ValidChars.indexOf(digit) == -1) {
			return false;
		}
	}
	
	// Add five 0 if the code has only 8 digits
	if (eanCode.length == 8 ) {
		eanCode = "00000" + eanCode;
	}
	// Check for 13 digits otherwise
	else if (eanCode.length != 13) {
		return false;
	}
	
	// Get the check number
	originalCheck = eanCode.substring(eanCode.length - 1);
	eanCode = eanCode.substring(0, eanCode.length - 1);
	
	// Add even numbers together
	even = Number(eanCode.charAt(1)) + 
	       Number(eanCode.charAt(3)) + 
	       Number(eanCode.charAt(5)) + 
	       Number(eanCode.charAt(7)) + 
	       Number(eanCode.charAt(9)) + 
	       Number(eanCode.charAt(11));
	// Multiply this result by 3
	even *= 3;
	
	// Add odd numbers together
	odd = Number(eanCode.charAt(0)) + 
	      Number(eanCode.charAt(2)) + 
	      Number(eanCode.charAt(4)) + 
	      Number(eanCode.charAt(6)) + 
	      Number(eanCode.charAt(8)) + 
	      Number(eanCode.charAt(10));
	
	// Add two totals together
	total = even + odd;
	
	// Calculate the checksum
    // Divide total by 10 and store the remainder
    checksum = total % 10;
    // If result is not 0 then take away 10
    if (checksum != 0) {
        checksum = 10 - checksum;
    }

	// Return the result
	if (checksum != originalCheck) {
		return false;
	}
	
    return true;
}

Note that this code can validate EAN-8 and EAN-13 barcodes.

,


  1. #1 by romtec on 06 Feb 2012 - 15:25

    Perfect, you saved my day!
    Exactly what i was looking for 🙂

  2. #2 by Vitalij on 07 Mar 2012 - 10:07

    Thanks a lot! Very helpfull!

  3. #3 by Jürgen on 21 Aug 2012 - 09:02

    Thank you very much – I´ll try to get it working, a very nice piece of code. And – thanks for sharing it.
    regards,
    Juergen

  4. #4 by Jose on 29 Oct 2012 - 01:52

    Thanks! 🙂

  5. #5 by David Eggman on 27 May 2013 - 16:33

    Based on this I made an alternative solution. This which will also add padding 0’s and checking for EAN8,12,13,14

    function checkEan(eanCode) {
    eanCode = eanCode.trim();
    if ([8,12,13,14].indexOf(eanCode.length) == -1 ) {
    // alert(‘Incorrect length’);
    return false; }
    if (eanCode.length < l) {
    eanCode = Array(14 – eanCode.length).join(0) + eanCode; //add 0's as padding
    }
    if (!eanCode.match(/[\d]{eanCode.length}/))
    {
    // alert('Illegal characters');
    return false; }
    var total=0;
    var a=eanCode.split('');
    for (var i in a)
    {
    if(i<(a.length-1))
    {
    total+=
    a[i] * (1 + (i % 2) * 2);
    }
    }
    if ((10 – (total % 10))%10 != eanCode.substring(eanCode.length – 1)) {
    // alert('Wrong checksum');
    return false;
    }
    return true;
    }

  6. #6 by smoreau on 27 May 2013 - 22:36

    Thanks for this David.

  7. #7 by wartur on 08 Dec 2013 - 21:38

    Thanks!!!!!!!

  8. #8 by Boris on 22 Jun 2015 - 09:20

    Thank you very much!

(will not be published)