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 | | | | re: Input validation for 8-digit UPC-E code
Kermit Piper wrote:[color=blue]
> 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.[/color]
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. | | | | re: Input validation for 8-digit UPC-E code
Kermit Piper wrote:[color=blue]
> 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?[/color]
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 | | | | re: Input validation for 8-digit UPC-E code
Thank you Rob.
Kermit Piper wrote:[color=blue]
> 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[/color] |  | Similar JavaScript / Ajax / DHTML bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,295 network members.
|