473,837 Members | 1,545 Online

# 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')}
}
</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.

KP

Feb 10 '06 #1
3 9335

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')}
}
</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

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';
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"
<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')}
}
</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.

KP

Feb 10 '06 #4

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