Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

length should not invalidate bar code. #9

Open
proseLA opened this issue Feb 24, 2023 · 1 comment
Open

length should not invalidate bar code. #9

proseLA opened this issue Feb 24, 2023 · 1 comment

Comments

@proseLA
Copy link

proseLA commented Feb 24, 2023

i have encountered manufacturers who are using a 2 digit suffix at the end of the 12 digit UPC bar code. the 2 digit suffix indicates a packaging of some sort. you are returning false if the length is not the expected length.

// Check if the string length matches the code's desired length
if (strlen($code) == $length) {

here is one such code: 84788101273501

upczilla does validate this code as true while your validator automatically returns false.

@proseLA
Copy link
Author

proseLA commented Feb 24, 2023

potential solution:

$strlen = strlen($code);
if ($strlen >= $length) {
  $offset = -1 + $length - $strlen;
  // If the string contains only digits
  if (preg_match('/^[0-9]+$/', $code)) {
	if (BarcodeValidator::calculateEANCheckDigit(substr($code, 0, $offset)) == substr($code, $offset, 1)) {
		return true;
	} else {
		return false;
	}
  } else // The code has invalid characters

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant