PHP VIN Code Validation

Since 1981 Vehicle Identification Numbers in North America have followed a standard which provides a check digit to aid in the identification of transcription errors. Reproduced below is my PHP implementation that calculates a checksum and compares it to the check digit.

The code begins by converting the string to uppercase and checking that the passed value matches a regex looking for exactly 13 uppercase alphanumeric characters, excluding the letters I, O and Q, followed by exactly 4 digits. On success the code proceeds to implement the weighted checksum algorithm using the private helper function getNumVal($char) as a lookup table. Note that the 9th character is the checksum, and thus excluded from the calculation.

Once the total is calculated it is divided by 11 and its remainder becomes the checksum, with ‘X’ representing 10. It should be obvious that the mod 11 operation provides limited protection against errors since there is a 1 in 11 chance an invalid VIN code will pass the test. Nevertheless, the algorithm has saved countless invalid VIN codes from making into my databases.

As shown below, it is important to do some cursory error checking in the web form before passing the input to this method. This greatly assists the user in determining the most likely causes of error.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.