| 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. |