473,804 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Input validation for 8-digit UPC-E code

Hello everyone,

OK, let me try and explain again please. 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(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 )">

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.

Thanks in advance,
KP

Feb 10 '06 #1
3 9333
VK

Kermit Piper wrote:
Hello everyone,

OK, let me try and explain again please. 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(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 )">

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.


Here is the exact algorithm to validate UPC-E code:
<http://www.makebarcode .com/specs/upc_e.html>

Convert it into sequence of JavaScript checks, show what you'll come
to.

P.S. It is usually better to stay within the original thread.

P.P.S. There isn't a direct conversion from your current validator to
the UPC-E validator. Therefore you're asking to write for you a program
from the scratch plus find an algorithm for you first. It is not
offensive at all in such case asking to show some initial efforts from
your side either.

Feb 10 '06 #2
Kermit Piper wrote:
Hello everyone,

OK, let me try and explain again please. 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?


The least you could do is find a valid 8 digit number (or a few of them)
to check against. Here is a script that:

Checks that the input is 8 characters.
Checks that the first digit is zero
Calculates the UPC check digit based on the 1st to 7th digits
Checks if the result is equal to the 8th digit.

You need to verify that the above agorithm is correct (in particular,
whether the first digit is included in the calculation of the check
digit - I've used it).

<script type="text/javascript">

// Check if string of digits is valid UPC-E code
function checkUPCE(n)
{
if (!/^\d{8}$/.test(n)) return 'Input must be 8 digits';
if ('0' != n.substring(0,1 )) return 'Input must start with 0';
return n.substring(7) == getUPCCheckDigi t(n.substring(0 ,7));
}

// Return check digit for a string of digits
function getUPCCheckDigi t(x)
{
x = (''+x).split('' );
for (var i=0, z=0, len=x.length; i<len; ++i){
z += (i%2)? +x[i] : x[i]*3;
}
z = z%10;
return (z)? 10-z : z;
}

</script>

<form action="">
<div>
<div><input type="text" name="inp01" value="02369761 ">
<input type="button" value="Check UPC-E"
onclick="alert( checkUPCE(this. form.inp01.valu e));">
<input type="reset">
</div>
</form>
[...]

--
Rob
Feb 10 '06 #3
Thank you Rob.
Kermit Piper wrote:
Hello everyone,

OK, let me try and explain again please. 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(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 )">

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.

Thanks in advance,
KP


Feb 10 '06 #4

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
2155
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
27
35956
by: code_wrong | last post by:
Visual Basic (not dot net) what is the best way to check the User has entered an integer into an InputBox? isNumeric() checks for a numeric value .. but does not notify of numbers with decimal places inputBox returns a string so I could check for decimal point??? this seems like overkill The value returned can be asigned into an Integer type and then a Single
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?
1
1295
by: Rakesh Chaurasia | last post by:
Hello, i join recently this group. I'm web developer(PHP) help me I have one problem in javascript i wanna validate these fields
0
9594
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
10600
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
10350
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
10351
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
10096
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
9174
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
6866
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5534
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
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.