469,293 Members | 1,335 Online

# Input validation for 10, 11 and 12-digit UPC Codes

Hello,

I have been searching, Googling, searching. Cannot find a javascript to
calc 10, 11 or 12 digit UPC codes. I just need an algorithm that calcs
to verify the correct check digit. I have found a few that add the
check digit then do the final calc, but I need an input validation that
will verify the UPC as being correct with the correct check digit after
the user simply enteres the number in one text box. There has to be
something out there, but darned if I can find it. I'd also settle for a
java api, but couldn't find any of those either. There's a few creidt
card format validators, but no UPC. If anyone knows of any I would
greatly appreciate it. Is this something that can be done witht the
modulus operator and regular expressions? I'd even pay for a third
party validation tool but I can't find ANYTHING!

Thanks,
KP

Feb 7 '06 #1
17 19739 Kermit Piper said the following on 2/7/2006 12:00 AM:
Hello,

I have been searching, Googling, searching. Cannot find a javascript to
calc 10, 11 or 12 digit UPC codes. I just need an algorithm that calcs
to verify the correct check digit.

This is for 12 digit only. If you can find a site that explains 10 or
11, post a URL.

Sample UPC Code it uses: 0 64200 11589 6

Step 1: Sum all of the digits in the odd positions together.

0+4+0+1+5+9 = 19

Step 2: Mutliply the sum from Step 1 by 3.

3 * 19 = 57

Step 3: Sum all of the digits in the even positions together.

6+2+0+1+8 = 17

Step 4: Sum together the results from Step 2 and Step 3.

17 + 57 = 74

Step 5: Subtract the sum from the next highest multiple of 10.

80 - 74 = 6 [check digit]

From this site:

Number 2 Hit for "Validate UPC Code".

Not sure how "correct" that information is though.

Sample script to do that:

<script type="text/javascript">
function validateUPCCode(UPCField){
var myArray = UPCField.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')}
}

</script>

<input type="text" onchange="validateUPCCode(this)">

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 7 '06 #2
Kermit Piper wrote:
Hello,

I have been searching, Googling, searching. Cannot find a javascript to
calc 10, 11 or 12 digit UPC codes. I just need an algorithm that calcs
to verify the correct check digit. I have found a few that add the
check digit then do the final calc, but I need an input validation that
will verify the UPC as being correct with the correct check digit after
the user simply enteres the number in one text box. There has to be
something out there, but darned if I can find it. I'd also settle for a
java api, but couldn't find any of those either. There's a few creidt
card format validators, but no UPC. If anyone knows of any I would
greatly appreciate it. Is this something that can be done witht the
modulus operator and regular expressions? I'd even pay for a third
party validation tool but I can't find ANYTHING!

Very first Google hit for "calculate UPC codes":

<URL:http://www.export911.com/e911/coding/digiCalc.htm>
the source is here:

<URL:http://www.export911.com/_bCode/checDigi.js>
Note:
//**Author: Morris Ng
--
Rob
Feb 7 '06 #3
RobG said the following on 2/7/2006 12:39 AM:
Kermit Piper wrote:
Hello,

I have been searching, Googling, searching. Cannot find a javascript to
calc 10, 11 or 12 digit UPC codes. I just need an algorithm that calcs
to verify the correct check digit. I have found a few that add the
check digit then do the final calc, but I need an input validation that
will verify the UPC as being correct with the correct check digit after
the user simply enteres the number in one text box. There has to be
something out there, but darned if I can find it. I'd also settle for a
java api, but couldn't find any of those either. There's a few creidt
card format validators, but no UPC. If anyone knows of any I would
greatly appreciate it. Is this something that can be done witht the
modulus operator and regular expressions? I'd even pay for a third
party validation tool but I can't find ANYTHING!

Very first Google hit for "calculate UPC codes":

<URL:http://www.export911.com/e911/coding/digiCalc.htm>
the source is here:

<URL:http://www.export911.com/_bCode/checDigi.js>
Note:
//**Author: Morris Ng

I like mine better :)

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 7 '06 #4

All you need is a regexp, what's the UPC string pattern criterion? Be

Danny
Feb 7 '06 #5
Randy Webb wrote:
RobG said the following on 2/7/2006 12:39 AM:
Kermit Piper wrote:
Hello,

I have been searching, Googling, searching. Cannot find a javascript to
calc 10, 11 or 12 digit UPC codes. I just need an algorithm that calcs
to verify the correct check digit. I have found a few that add the
check digit then do the final calc, but I need an input validation that
will verify the UPC as being correct with the correct check digit after
the user simply enteres the number in one text box. There has to be
something out there, but darned if I can find it. I'd also settle for a
java api, but couldn't find any of those either. There's a few creidt
card format validators, but no UPC. If anyone knows of any I would
greatly appreciate it. Is this something that can be done witht the
modulus operator and regular expressions? I'd even pay for a third
party validation tool but I can't find ANYTHING!

Very first Google hit for "calculate UPC codes":

<URL:http://www.export911.com/e911/coding/digiCalc.htm>
the source is here:

<URL:http://www.export911.com/_bCode/checDigi.js>
Note:
//**Author: Morris Ng

I like mine better :)

Yeah, it's really awful! Must be paid by the yard... the following
replicates the essential logic as far as I can tell, it gives the same
results for the same test set but I can't vouch for the algorithm, which
is very similar to (the same as?) yours.

I've used 'sumEven' for the elements with even indexes (0-10), which
equates to your odd digits (1-11). Same logic for sumOdd.
<script type="text/javascript">

function calcUPC(x)
{
// Validate input is 11 digits
if (!/^\d{11}\$/.test(x)){
return '';
}

// Turn into array and do sums
x = x.split('');
var evenSum = 0, oddSum = 0;
for (var i=0, len=x.length; i<len; ++i){
(i%2)? oddSum += +x[i] : evenSum += +x[i];
}
var z = (evenSum*3 + oddSum)%10;

// Calc check digit and return it
return (z)? 10-z : z;
}

</script>

<form action="">
<input type="text" name="inVal">
<input type="text" name="outVal">
<input type="button" value="Calculate check digit"
onclick="this.form.outVal.value = calcUPC(this.form.inVal.value);">
</form>

--
Rob
Feb 7 '06 #6
Thanks Randy, this works great! Do you think it would be possible to
modify this to validate 10 and 11-digit UPC codes as well?

Thanks,
KP

Feb 7 '06 #7
Kermit Piper said the following on 2/7/2006 2:59 PM:

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
Thanks Randy, this works great! Do you think it would be possible to
modify this to validate 10 and 11-digit UPC codes as well?

If you can find a site that explains how the checksum in 10 and 11 digit
UPC Codes works, sure.

Maybe later I will look at the site Rob found and see what it says about
10 and 11 digit UPC's. The one I found only explained 12 digits.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 7 '06 #8
Danny said the following on 2/7/2006 1:06 AM:
All you need is a regexp,

You should really consider what you write before you post it. But, I
will give you the benefit of the doubt and ask you for a RegExp that
will sum the digits in even charAt positions and sum the digits in odd
charAt positions. After all, that is how checksums work.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 7 '06 #9
Thanks for everyone's input. the 12-digit function works, now I just
need to find one for UPC-E (I have a conversion script for UPC-A to
UPC-E, but I'd like to try and find a straight, simple function like
the one posted here for the 12-digit. The conversino script only takes
in the middle 6 digits, which means I would have to go through and
strip digits, etc. It's already getting too messy. Thanks again
everyone.

Feb 7 '06 #10
JRS: In article <Rc********************@comcast.com>, dated Tue, 7 Feb
2006 00:43:35 remote, seen in news:comp.lang.javascript, Randy Webb
<Hi************@aol.com> posted :
Step 5: Subtract the sum from the next highest multiple of 10. step5 = ((Math.floor(step4/10) + 1)*10) - step4;

FWIW: step5 = (800 - step4) % 10 // probably equivalent

If speed really mattered, I'd try not splitting the string into lots of
stringlets, but converting it to Number and obtaining the successive
digits by arithmetic -- it ***might*** be quicker. The effort would not
benefit the application, but might be instructive.

If any Julian Calendar users are around : js-date8.htm#JCDR is better.

--
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.merlyn.demon.co.uk/clpb-faq.txt> RAH Prins : c.l.p.b mFAQ;
<URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.
Feb 8 '06 #11
Kermit Piper wrote:
Thanks for everyone's input. the 12-digit function works, now I just
need to find one for UPC-E (I have a conversion script for UPC-A to
UPC-E, but I'd like to try and find a straight, simple function like
the one posted here for the 12-digit. The conversino script only takes
in the middle 6 digits, which means I would have to go through and
strip digits, etc. It's already getting too messy. Thanks again
everyone.

According to this reference below, the algorithm for UPC-E is exactly
the same as that for UPC-A. It consists of 8 characters where the first
is always zero and the last is the check digit.

<URL:http://www.tkb-4u.com/code/barcode/upc.php>

So the following function will return the check digit given any string
of digits (it's up to you to pre-validate the input):

function getUPCCheckDigit(x)
{
x = '' + x; // Make sure x is a string
x = x.split('');
var sum = 0;
for (var i=0, len=x.length; i<len; ++i){
(i%2)? sum += +x[i] : sum += x[i]*3;
}
var z = sum%10;
return (z)? 10-z : z;
}
If you want a script to convert UPC-A to E, provide an algorithm for
selecting the appropriate digits. Based on what you have supplied
above, the following changes UPC-A to E by taking the 'middle 6' digits,
pre-pending a zero and appending a check digit:

function UPCa2e(x)
{
x += ''; // Make sure x is a string
var y = x.substring(3,9);
return '0' + y + getUPCCheckDigit(y);
}

--
Rob
Feb 8 '06 #12
Dr John Stockton said the following on 2/7/2006 5:46 PM:
JRS: In article <Rc********************@comcast.com>, dated Tue, 7 Feb
2006 00:43:35 remote, seen in news:comp.lang.javascript, Randy Webb
<Hi************@aol.com> posted :
Step 5: Subtract the sum from the next highest multiple of 10.
step5 = ((Math.floor(step4/10) + 1)*10) - step4;

FWIW: step5 = (800 - step4) % 10 // probably equivalent

Only if step4 is in the range of 71-79. For it to be general, the 800
would have to be programatically determined based on step4.

I didn't use the modulus because the OP explicitly asked not to :)
If speed really mattered, I'd try not splitting the string into lots of
stringlets, but converting it to Number and obtaining the successive
digits by arithmetic -- it ***might*** be quicker. The effort would not
benefit the application, but might be instructive.

Absolutely. Mine was more to show the OP how to do it than come up with
the most efficient solution but you are correct.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 8 '06 #13
Kermit Piper said the following on 2/7/2006 6:00 PM:
Thanks for everyone's input.
Thanks for quoting in the future.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
the 12-digit function works, now I just
need to find one for UPC-E (I have a conversion script for UPC-A to
UPC-E, but I'd like to try and find a straight, simple function like
the one posted here for the 12-digit. The conversino script only takes
in the middle 6 digits, which means I would have to go through and
strip digits, etc. It's already getting too messy. Thanks again
everyone.

Converting from a 12-digit to a 6-digit code is trivial compared to
validating the checksum.

var twelveDigitCode = "123456789012";
var sixDigitCode = twelveDigitCode.substring(3,9);

You might have to change the 3 and 9 to get the proper 6 digits.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

Feb 8 '06 #14
JRS: In article <p8******************************@comcast.com>, dated
Tue, 7 Feb 2006 16:47:25 remote, seen in news:comp.lang.javascript,
Randy Webb <Hi************@aol.com> posted :
Danny said the following on 2/7/2006 1:06 AM:
All you need is a regexp,

You should really consider what you write before you post it. But, I
will give you the benefit of the doubt and ask you for a RegExp that
will sum the digits in even charAt positions and sum the digits in odd
charAt positions. After all, that is how checksums work.

S = "9876"
S = S.replace(/^(\d)(\d)(\d)(\d)\$/, "\$1*3 + \$2 + \$3*3 + \$4")
Tot = eval(S) // S = "9*3 + 8 + 7*3 + 6" => 62

shows how a RegExp can be used for this sort of thing. With a little
more effort, the RegExp could generate code to calculate the check
digit, but there's no need for that. And

S = "9876543210"
S = S.replace(/(\d\d)/g, "\$1 ").replace(/(\d)(\d) /g, "\$1*3 + \$2 + ")
// S = "9*3 + 8 + 7*3 + 6 + 5*3 + 4 + 3*3 + 2 + 1*3 + 0 + "
Tot = eval(S + "0") // 95

Granted, a little more than a RegExp is needed.

It may be that a subtler RegExp will do the general case in a single go;
and it may be that shorter UPCs can be handled with long-UPC code by
appending zeroes. If there is an *authoritative* *HTML* definition of
all UPCs, I might add a section to js-misc?.htm.

I think this use of eval is compliant with FAQ 4.40; if the author had
meant "absolutely necessary" he'd have used both words.
In general, given an unchecked product code, *someone* has to calculate
a checksum and append it. But when validating a checked code it may be
possible, depending on design, to treat the check digit like the others
and test for a specific result - as in parity-checking 8-hole tape, for
example.

--
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
Feb 8 '06 #15
JRS: In article <bP********************@comcast.com>, dated Wed, 8 Feb
2006 10:49:45 remote, seen in news:comp.lang.javascript, Randy Webb
<Hi************@aol.com> posted :
Dr John Stockton said the following on 2/7/2006 5:46 PM:
JRS: In article <Rc********************@comcast.com>, dated Tue, 7 Feb
2006 00:43:35 remote, seen in news:comp.lang.javascript, Randy Webb
<Hi************@aol.com> posted :
Step 5: Subtract the sum from the next highest multiple of 10.
step5 = ((Math.floor(step4/10) + 1)*10) - step4;

FWIW: step5 = (800 - step4) % 10 // probably equivalent

Only if step4 is in the range of 71-79. For it to be general, the 800
would have to be programatically determined based on step4.

But, because of the mod 10, the 800 can be any multiple of 10 that's
bigger than step4. The biggest step4 seemed to be around 200, so
originally I thought to write 1000 ; but I decided to save a character.
However, I should have used 1e3.
Actually, the two methods agree except that yours gives 10 where mine
gives 0. Clearly 0 is more logical, but 10 may be right. If so, change
mine to
step5 = 1 + (799 - step4) % 10

I didn't use the modulus because the OP explicitly asked not to :)

I took "witht" as meaning "with", not "without", which would be
unreasonable.
--
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
Feb 9 '06 #16
Hello everyone,

OK, let me try again. Here is what I'm trying to do. I have a 12-digit
(UPC-A) javascript validation script which works great. All I need now
is a similar script for 8-digit (UPC-E)type script? I don't want to
convert it to a 12-digit UPC-A before validating it as a 12-digit
number. I would prefer to just be able to calc the number, as an
8-digit number, check digit and all, just like I am currently doing
successfully with this 12-digit validation script:

<script type="text/javascript">
function validateUPCCode(UPCField){
var myArray = UPCField.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')}
}
</script>

<input type="text" onchange="validateUPCCode(this)">

I don't know what the string pattern criteria would be, all I know is I
need to be able to verify that the 8-digit number entered is a valid
format for a UPC-E number, which as I understand is based on the check
digit.

Just a straightforward script like I have above for the 12-digit
validation, only for an 8-digit validation. If this is possible I sure
would appreciate if someone could show me how.

KP

Danny wrote:
All you need is a regexp, what's the UPC string pattern criterion? Be

Danny

Feb 10 '06 #17
, dated Thu, 9 Feb 2006 23:08:47 remote, seen in
news:comp.lang.javascript, Kermit Piper <di***********@hotmail.com>
posted :

Just a straightforward script like I have above for the 12-digit
validation, only for an 8-digit validation. If this is possible I sure
would appreciate if someone could show me how.

You should first do your part.

Find an authoritative HTML or plain-text definition of the relevant UPC
formats, and provide a link to it.

The actual coding is doubtless easy enough, though there may be debate
here as to how *best* to do it.

Your code is badly laid out and unfit for posting for public viewing.
Code should be indented to show the intended structure and spaces should
be used fairly freely for legibility. Look at how the regulars here
write code, and follow their example.

Remember : if you don't bother to present your question in a helpful
manner, others are less likely to bother with helping you.

--