HEELPBOOK - Validate your EAN barcode (Javascript) ###################### Note that this code can validate EAN-8 and EAN-13 barcodes. 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; } ############ ARTICLE INFO ############# Article Month: February Article Date: 26/02/2013 Permalink: http://heelpbook.altervista.org/2013/validate-your-ean-barcode-javascript/ Source: http://www.logikdev.com/2010/05/13/validate-your-ean-barcode/ Language: English View more articles on: http://www.heelpbook.net/ Follow us on Facebook: http://it-it.facebook.com/pages/HeelpBook/100790870008832 Follow us on Twitter: https://twitter.com/#!/HeelpBook Follow us on RSS Feed: http://feeds.feedburner.com/Heelpbook Follow us on Delicious: http://delicious.com/heelpbook Linkedin: http://it.linkedin.com/pub/stefano-maggi/27/73a/b20 Google+ : https://plus.google.com/116990277568167008289/posts