473,805 Members | 2,164 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 20506
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:

<URL: http://www.cs.queensu.ca/~bradbury/c...t/upccheck.htm >

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(st ep4/10) + 1)*10) - step4;
if (step5 == myArray[myArray.length-1]){alert('valid UPC Code')}
else{alert('inv alid UPC Code')}
}

</script>

<input type="text" onchange="valid ateUPCCode(this )">

--
Randy
comp.lang.javas cript 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.c om/e911/coding/digiCalc.htm>
the source is here:

<URL:http://www.export911.c om/_bCode/checDigi.js>
Note:
//**All rights reserved
//**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.c om/e911/coding/digiCalc.htm>
the source is here:

<URL:http://www.export911.c om/_bCode/checDigi.js>
Note:
//**All rights reserved
//**Author: Morris Ng


I like mine better :)

--
Randy
comp.lang.javas cript 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
as concise as possible please.

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.c om/e911/coding/digiCalc.htm>
the source is here:

<URL:http://www.export911.c om/_bCode/checDigi.js>
Note:
//**All rights reserved
//**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)){
alert("Enter 11 digits");
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="Calculat e check digit"
onclick="this.f orm.outVal.valu e = calcUPC(this.fo rm.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:

Please quote what you are replying to.

If you want to post a followup via groups.google.c om, 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
article headers.
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.javas cript 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.javas cript 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1447
by: Tonta | last post by:
Hi I wanted to know how i could validate a users input. I.e to ensure that all data that is entered into a textbox is an interger, text, etc? can anyone point me to some useful articles im using vb.net to create a windows applicataion.
8
2039
by: Calan | last post by:
I have a server-side ASP script that dynamically creates an input form from a database table. The table contains a field name, the table where values are stored, type of input control, value for a label, etc. What I need to do is create a JS validation routine that will check each control for valid input, regardless of what the control name is. If it is a "select", it needs to verify the index is > 1. If it is an "input", it needs to...
3
1897
by: Vaughn | last post by:
In my MDI, I have a child form (frm_emp) that on a button click, displays another child form w/ a listview (frm_list). I had two questions: 1) Assuming I already have the emplcode the user clicked on frm_list, how would I be able to transfer that value back to frm_emp? 2) From within the double-clicked event in the frm_list, can I send the emplcode value to frm_emp *and* execute a public method in frm_emp. It'd be basically something...
4
2156
by: John Slate | last post by:
I have built a simple web form that uses input validation. I use the EnableClientScript option to produce a javascript alert box when input errors occur. The only validation is a password confirmation which uses a compare validator and a validation summary. On my development server, this form performs as designed. However, when deployed on a live server the input validation no longer works. I am accessing both live and development servers...
1
1289
by: John Slate | last post by:
I have built a simple form that uses input validation. I use the EnableClientScript option to produce a javascript alert box when input errors occur. The only validation is a password confirmation which uses a compare validator and a validation summary. On my local machine, this form performs as designed. However, when deployed on a live server the input validation no longer works. Any suggestions as to why this may be happening? Thanks!
2
1149
by: Buddy Ackerman | last post by:
I have a form into which users will enter text. I want the user to be able to enter "some" HTML however I would like to prevent "bad" HTML. The "bad" HTML would be things like <SCRIPT>, <OBJECT>, <APPLET>, etc. Does anyone know of a good server side validator that will catch this type of "bad" HTML input while allowing the acceptable input? --Buddy
3
3367
by: redefined.horizons | last post by:
I'm new to C#, (coming from a Java programming background), and I have a question about the correct use of exceptions. My book "Programming C#" by O'Reilly says the following in Chapter 11, "Handling Exceptions", on page 245: "An error is caused by user action. For example, the user might enter a number where a letter is expected. Once again, an error might cause an exception, but you can still prevent that by catching errors with...
1
1077
by: JP2006 | last post by:
I am using asp.net input validation on a web form to check user input. The web form is on a web user control which is then dragged on to a ..aspx page. The .aspx page also contains other web user controls in the way of various LinkButtons. The problem is that although the validation fires correctly when a user incorrectly completes the form it also fires when the user clicks on a LinkButton on that page!
1
1193
by: mstpaige | last post by:
How do I go about starting to input validation into my form for shipping weight and cost?
2
1549
by: poreko | last post by:
I am trying to validate the inputs of a form using javascript. The validation is working but the form is not been submitted after the inputs have been checked. I have been unable to find any error. Any help will be appreciated. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>FORM</title>
0
9718
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10613
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5544
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3846
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.