VIN Validation - Part I

Validating VIN's is just as important as validating any other peice of data in a program. You will find a routine that is derived from the widely known algorithm, which the details are published on Wikipedia. This code is released under GNU 3.0 license. You can utilize this code within your project.

This routine is the first step in validating a VIN by verifying the value of the check digit, which is encoded in the 9th position within the VIN. 

//Copyright shapemetrics 2010

using System;
using System.Collections;
using System.Collection.Specialized;
public class VIN
    {


        //Make sure no instance of this class is created... only method is static. 
        private VIN() { }

        public static bool IsValidVin(string p_strVin)
        {
            bool blnIsValid = false;
            int intValue = 0;
            //Multiplier weights for each position.
            int[] intWeights = { 8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2 };
            

            if (p_strVin == null || p_strVin.Length != 17)
            {
                return false;
            }

            //Capitalize all values in VIN
            p_strVin = p_strVin.ToUpper().Trim();
            int intCheckValue = 0;
            //Get the Check digit from VIN
            char check = p_strVin[8];
            //Get the Year from the VIN
            char year = p_strVin[9];
            //Ensure the check digit is 0-9 or X
            if (!char.IsDigit(check) && check != 'X')
            {
                return blnIsValid;
            }
            else
            {
                //Get the numeric value of the check digit
                if (check != 'X')
                {
                    char[] d = new char[] { check };
                    intCheckValue = int.Parse(Encoding.ASCII.GetString(Encoding.ASCII.GetBytes(d)));
                }
                else
                {
                    intCheckValue = 10;
                }
            }
            //Hash table of the character replacement values
            Hashtable replaceValues = new Hashtable();
            replaceValues.Add('A', 1);
            replaceValues.Add('B', 2);
            replaceValues.Add('C', 3);
            replaceValues.Add('D', 4);
            replaceValues.Add('E', 5);
            replaceValues.Add('F', 6);
            replaceValues.Add('G', 7);
            replaceValues.Add('H', 8);
            replaceValues.Add('J', 1);
            replaceValues.Add('K', 2);
            replaceValues.Add('L', 3);
            replaceValues.Add('M', 4);
            replaceValues.Add('N', 5);
            replaceValues.Add('P', 7);
            replaceValues.Add('R', 9);
            replaceValues.Add('S', 2);
            replaceValues.Add('T', 3);
            replaceValues.Add('U', 4);
            replaceValues.Add('V', 5);
            replaceValues.Add('W', 6);
            replaceValues.Add('X', 7);
            replaceValues.Add('Y', 8);
            replaceValues.Add('Z', 9);
            replaceValues.Add('1', 1);
            replaceValues.Add('2', 2);
            replaceValues.Add('3', 3);
            replaceValues.Add('4', 4);
            replaceValues.Add('5', 5);
            replaceValues.Add('6', 6);
            replaceValues.Add('7', 7);
            replaceValues.Add('8', 8);
            replaceValues.Add('9', 9);
            replaceValues.Add('0', 0);

            //Make sure it is a Valid Year 
            if (!replaceValues.Contains(year) && year != '0')
            {
                return blnIsValid;
            }

            //Make sure characters valid values. 
            for (int i = 0; i < p_strVin.Length; i++)
            {
                if (!replaceValues.Contains(p_strVin[i]))
                {
                    return false;
                }
                intValue += (intWeights[i] * ((int)replaceValues[p_strVin[i]]));
            }

            if ((intValue % 11) == intCheckValue)
            {
                blnIsValid = true;
            }

            return blnIsValid;
        }
    }

Newsletter Sign-up

Join our newsletter for product information and updates.