UPC validation doesn't work if check digit is 0

Posted on 2006-04-07
Last Modified: 2008-01-09

I have this algorithm which works for all 12-digit UPC codes, unless the final check digit is 0, in which case the validation always comes back as an invalid UPC code. If someone could look through and offer a suggestion or solution I would greatly appreciate it.

function checkUPCFormat12(formObj)
      var temp = formObj.value.trim();
      if (temp.length >11 && temp.length<= 12)            
            var myArray = formObj.value.split('');
            step1 = 0;
            var tempVar = myArray.length;
            for (var i=0;i<tempVar;i=i+2)
            step1 = step1 + +myArray[i];
            var step2=step1*3;
            step3 = 0;
            for(var i=1;i<tempVar-1;i=i+2)
            step4 = step2 + step3;
            step5 = ((Math.floor(step4/10) + 1)*10) - step4;
            if (step5 == myArray[myArray.length-1])
            //alert('valid UPC Code')
            return true;
            alert('You have entered an invalid 12-digit UPC Code. Verify and enter ALL numbers.')
            return false;

Question by:RickJa
    LVL 12

    Expert Comment

    LVL 63

    Accepted Solution

    From my point of view is check digit zero not allowed.
    But if you want it, here my version:


    function checkUPCFormat12(formObj){
      //var num = formObj.value.split(""); // this is for form element
      var num = formObj.split("");  // this is for String parameter
      if(num.length!=12) return false;
      var sum = 0;
      for(var sum=i=0;i<11;i++){
        if(i%2) sum += num[i]*1;
        else sum += num[i]*3;
      if((10-sum%10)%10==num[11]) return true;
      return false;



    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
    JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now