473,394 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

Thanks in advance,
KP

Feb 10 '06 #1
3 9296
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(step4/10) + 1)*10) - step4;
if (step5 == myArray[myArray.length-1]){alert('valid UPC Code')}
else{alert('invalid 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.


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) == getUPCCheckDigit(n.substring(0,7));
}

// Return check digit for a string of digits
function getUPCCheckDigit(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.value));" >
<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(step4/10) + 1)*10) - step4;
if (step5 == myArray[myArray.length-1]){alert('valid UPC Code')}
else{alert('invalid 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.

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
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...
8
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...
3
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...
4
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...
1
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...
2
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>,...
27
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...
1
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...
1
by: mstpaige | last post by:
How do I go about starting to input validation into my form for shipping weight and cost?
1
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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,...
0
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...
0
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,...
0
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...
0
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...

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.