//upc / gtin validation function checkGTIN(curFld) { if (ChkFldNums(curFld)== false) { // UPC not all numbers, return false alert ('UPC/GTIN number contains one or more letters.'); return false; } var fldval = curFld.value; // get the last digit for the check number, get the rest for the upc/gtin to verify var chkgtin = fldval.substr(0, fldval.length-1); // the gtin number of fldval / upc passed - the upc minus the check number var chkdigit = fldval.substr(fldval.length-1, fldval.length); // the upc/gtin to verify, all the chk digit of fldval // length must be 8, 12, 13, 14, or 18 including trailing check number if (fldval.length !=8 && fldval.length !=12 && fldval.length !=13 && fldval.length !=14 && fldval.length !=18) { // cannot check this upc, invalid format alert ('UPC/GTIN number is not the correct length. Also, verify you entered the UPC with the check digit.') return false; } // if 8 digits, check that first digit is 0 if (fldval.length == 8) { if(fldval.substring(0,1) != '0') { // must start w/0, return false alert ('An 8-digit U.S. UPC-E number must start with a leading 0.') return false; } else { // gtin-8 starts w/0 - valid for US product // convert to upc-a chkgtin = '0' + UPCEtoA(chkgtin); // replace field value curFld.value = '0' + chkgtin + chkdigit; // done, conversion, check digit is for upc-a, validation will be done w/gtin validation below } } // take gtin code (w/o check digit and get check digit) cc = GETGTINCheckDigit(chkgtin) // compare the checksum number and the final gtin/upc digit if (cc == chkdigit) { // match return true; } else { // not matched, not correct upc/gtin alert ('UPC number and its check digit do not match. Please check the number and re-enter it.'); return false; } } // get UPC check digit for UPC w/o digit function GETGTINCheckDigit(ccode) { ccode = ('' + ccode).split(''); // convert number to string if needed for (var i=0, z=0, len=ccode.length; i