473,837 Members | 1,436 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 20512
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
1448
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
2040
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
1898
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
2157
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
1150
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
9843
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
10882
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
10577
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...
1
10630
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10275
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5670
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
5851
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4476
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4045
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.