UPC validation doesn't work if check digit is 0

Hello,

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)
            {
            step3=step3+(+myArray[i]);
            }
            step4 = step2 + step3;
            step5 = ((Math.floor(step4/10) + 1)*10) - step4;
            if (step5 == myArray[myArray.length-1])
            {
            //alert('valid UPC Code')
            return true;
            }
            else
            {
            alert('You have entered an invalid 12-digit UPC Code. Verify and enter ALL numbers.')
            return false;
            }
      }

Thanks,
R
RickJaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

<script>

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;
}

alert(checkUPCFormat12("064200115896"));
alert(checkUPCFormat12("012345678905"));
alert(checkUPCFormat12("012345678400"));
</script>

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.