Barcode
Zend_Validate_Barcode allows you to check if a given value can be
represented as barcode.
Zend_Validate_Barcode supports multiple barcode standards and can be
extended with proprietary barcode implementations very easily. The following barcode
standards are supported:
CODE25: Often called "two of five."
This barcode has no length limitation, but it must contain an even amount of
characters. It supports only digits, and the last digit can be an optional checksum
which is calculated with modulo 10. This standard is used worldwide and common on
the market.
CODE39: CODE39 is one of the oldest available codes.
This barcode has a variable length. It supports digits, alphabetical characters
and 7 special characters. It can have an optional checksum which is calculated with
modulo 43. This standard is used worldwide and common within the industry.
CODE93: CODE93 is the successor of CODE39.
This barcode has a variable length. It supports digits, alphabetical characters
and 7 special characters. It has an checksum which is calculated with modulo 47 and
contains 2 characters. This standard produces a denser code than CODE39 and is more
secure.
EAN8: EAN is the shortcut for "European Article Number".
These barcodes must have a length of 8 characters. It supports only digits, and
the last digit is always a checksum. This standard is used worldwide but has a very
limited range. It can be found on small articles where a longer barcode could not
be printed.
EAN12: EAN is the shortcut for "European Article Number".
This barcode must have a length of 12 characters. It supports only digits, and the
last digit is always a checksum which is calculated with modulo 10. This standard is
used within the USA and common on the market. It has been superceded by EAN13.
EAN13: EAN is the shortcut for "European Article Number".
This barcode must have a length of 13 characters. It supports only digits, and the
last digit is always a checksum which is calculated with modulo 10. This standard is
used worldwide and common on the market.
EAN14: EAN is the shortcut for "European Article Number".
This barcode must have a length of 14 characters. It supports only digits, and the
last digit is always a checksum which is calculated with modulo 10. This standard is
used worldwide and common on the market. It is the successor for EAN13.
GTIN12: GTIN is the shortcut for "Global Trade Item Number".
This barcode uses the same standard as EAN12 and is its successor. It's commonly
used within the USA.
GTIN13: GTIN is the shortcut for "Global Trade Item Number".
This barcode uses the same standard as EAN13 and is its successor. It is used
worldwide by industry.
GTIN14: GTIN is the shortcut for "Global Trade Item Number".
This barcode uses the same standard as EAN14 and is its successor. It is used
worldwide and common on the market.
ITF14: ITF14 is the GS1 implementation of an Interleaved Two
of Five bar code.
This barcode is a special variant of Interleaved 2 of 5. It must have a length of
14 characters and is based on GTIN14. It supports only digits, and the last digit
must be a checksum digit which is calculated with modulo 10. It is used worldwide
and common within the market.
SSCC: SSCC is the shortcut for "Serial Shipping Container
Code".
This barcode is a variant of EAN barcode. It must have a length of 18 characters and
supports only digits. The last digit must be a checksum digit which is calculated
with modulo 10. It is commonly used by the transport industry.
UPCA: UPC is the shortcut for "Univeral Product Code".
This barcode preceeded EAN13. It must have a length of 12 characters and supports
only digits. The last digit must be a checksum digit which is calculated with
modulo 10. It is commonly used within the USA.
UPCE: UPCE is the short variant from UPCA.
This barcode is a smaller variant of UPCA. It must have a length of 6 characters and
supports only digits. There is no checksum within this barcode. It is commonly used
with small products where a UPCA barcode would not fit.
Basic usage
To validate if a given string is a barcode you just need to know its type. See the
following example for an EAN13 barcode:
isValid($input)) {
// input appears to be valid
} else {
// input is invalid
}
]]>
Optional checksum
Some barcodes can be provided with an optional checksum. These barcodes would be valid
even without checksum. Still, when you provide a checksum, then you should also validate
it. By default, these barcode types perform no checksum validation. By using the
checksum option you can define if the checksum will be validated or
ignored.
'EAN13',
'checksum' => false,
));
if ($valid->isValid($input)) {
// input appears to be valid
} else {
// input is invalid
}
]]>
Reduced security by disabling checksum validation
By switching off checksum validation you will also reduce the security of the used
barcodes. Additionally you should note that you can also turn off the checksum
validation for those barcode types which must contain a checksum value. Barcodes
which would not be valid could then be returned as valid even if they are not.
Writing custom adapters
You may write custom barcode validators for usage with
Zend_Validate_Barcode; this is often necessary when dealing with
proprietary barcode types. To write your own barcode validator, you need the following
information.
Length: The length your barcode must have. It can have one
of the following values:
Integer: A value greater 0, which means that the
barcode must have this length.
-1: There is no limitation for the length of this
barcode.
"even": The length of this barcode must have a
even amount of digits.
"odd": The length of this barcode must have a
odd amount of digits.
array: An array of integer values. The length of
this barcode must have one of the set array values.
Characters: A string which contains all allowed characters
for this barcode. Also the integer value 128 is allowed, which means the first
128 characters of the ASCII table.
Checksum: A string which will be used as callback for a
method which does the checksum validation.
Your custom barcode validator must extend
Zend_Validate_Barcode_AdapterAbstract or implement
Zend_Validate_Barcode_AdapterInterface.
As an example, let's create a validator that expects an even number of characters that
include all digits and the letters 'ABCDE', and which requires a checksum.
isValid($input)) {
// input appears to be valid
} else {
// input is invalid
}
]]>