The Code Behind Barcodes

4.3 Functions

Now we have all the data we will need for now. Time to start making functions!

We need to make a function to fetch and validate the user-input UPC number. We will use two functions for validation. isValidUPC will accept the barcode value and determine if it is the correct length as well as a valid value. calculateCheckDigit will calculate the check digit for the barcode value supplied using the formula above, as its name implies. preprocessUPC has a big job. It will fetch the user-input UPC value, validate the value using the validation functions mentioned above, and display any errors if the value is not valid.

We will need several functions to render our valid barcode to make this possible. fillBG will fill the background. This function is essential on non-white web pages.