Jump to content

Verify UPC Check Digit


Recommended Posts

I am building an item entry form where the user will need to submit a products corresponding UPC (11 or 12 digit upc) with the check digit. I am looking for a way, in caspio, to verify that the check digit is correct. 

For example, to calculate the check digit for UPC 82546692620

  1. I will take the sum of all the odd position digits (8+5+6+9+6+0=34)
  2. I will multiply the sum by 3 (34 * 3 = 102)
  3. I will take the sum of all the even position digits (2+5+6+2+2=16)
  4. I will add the result of step 2 with step 3 (102+16=118)
  5. I will divide the sum of step 4 by 10 (118/10=11.8 or 11 remainder 8)
  6. I will take the remainder of step 5 and subtract from 10 (10-8=2)
  7. The resulting check digit for 82546692620 is 2

 

I have two fields that the user will populate - UPC and UPC Check Digit. I would need to calculate the result above and ensure it is equal to the check digit entered by the user. If not equal, the form should not submit.

Any assistance would be most appreciated and helpful.

Link to comment
Share on other sites

I found this scripting that might be helpful. I am uncertain how to translate this for Caspio.

 

function checkEan(eanCode) {
	// Check if only digits
	var ValidChars = "0123456789";
	for (i = 0; i < eanCode.length; i++) { 
		digit = eanCode.charAt(i); 
		if (ValidChars.indexOf(digit) == -1) {
			return false;
		}
	}
	
	// Add five 0 if the code has only 8 digits
	if (eanCode.length == 8 ) {
		eanCode = "00000" + eanCode;
	}
	// Check for 13 digits otherwise
	else if (eanCode.length != 13) {
		return false;
	}
	
	// Get the check number
	originalCheck = eanCode.substring(eanCode.length - 1);
	eanCode = eanCode.substring(0, eanCode.length - 1);
	
	// Add even numbers together
	even = Number(eanCode.charAt(1)) + 
	       Number(eanCode.charAt(3)) + 
	       Number(eanCode.charAt(5)) + 
	       Number(eanCode.charAt(7)) + 
	       Number(eanCode.charAt(9)) + 
	       Number(eanCode.charAt(11));
	// Multiply this result by 3
	even *= 3;
	
	// Add odd numbers together
	odd = Number(eanCode.charAt(0)) + 
	      Number(eanCode.charAt(2)) + 
	      Number(eanCode.charAt(4)) + 
	      Number(eanCode.charAt(6)) + 
	      Number(eanCode.charAt(8)) + 
	      Number(eanCode.charAt(10));
	
	// Add two totals together
	total = even + odd;
	
	// Calculate the checksum
    // Divide total by 10 and store the remainder
    checksum = total % 10;
    // If result is not 0 then take away 10
    if (checksum != 0) {
        checksum = 10 - checksum;
    }

	// Return the result
	if (checksum != originalCheck) {
		return false;
	}
	
    return true;
}

 

Source: http://www.logikdev.com/2010/05/13/validate-your-ean-barcode/

Link to comment
Share on other sites

  • 3 months later...

Did you ever figure this out? I'm doing almost the exact same thing as you were trying to do here.... I'm just struggling to do it on the client side.... I keep getting the answer to contact Caspio and do this server side, but I'd REALLY like to avoid paying extra for something that should be simple to do!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...