VIN Validation - Part III
In previous articles, I published the C# source code to validate VIN & how the algorithm works. In this blog, I am going to cover an adapted algorithm to over come the weakness in some of the VIN algorithm. This algorithm assumes that the VIN has already been validated. It returns a keying value that can be used to find the correct VIN.
//Copyright Rusty Davis 2010
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
public class VinValidation
{
public string VinKey(string p_strVin)
{
string strValue = "";
string strWMI = "";
string strWMI_Ext = "";
p_strVin = p_strVin.ToUpper().Trim();
if (p_strVin.Length != 17)
{
return strValue;
}
ArrayList aryValidCheckDigits = new ArrayList();
aryValidCheckDigits.Add('0');
aryValidCheckDigits.Add('1');
aryValidCheckDigits.Add('2');
aryValidCheckDigits.Add('3');
aryValidCheckDigits.Add('4');
aryValidCheckDigits.Add('5');
aryValidCheckDigits.Add('6');
aryValidCheckDigits.Add('7');
aryValidCheckDigits.Add('8');
aryValidCheckDigits.Add('9');
aryValidCheckDigits.Add('X');
char check = p_strVin[8];
if (!char.IsNumber(check) && check != 'X')
{
return strValue;
}
if (!aryValidCheckDigits.Contains(check))
{
return strValue;
}
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);
for (int i = 0; i < p_strVin.Length; i++)
{
if (!replaceValues.Contains(p_strVin[i]))
{
return strValue;
}
}
strWMI = p_strVin.Substring(0, 3);//Get the WMI
if (strWMI.EndsWith("9"))
{
strWMI_Ext = p_strVin.Substring(11, 3);
}
for (int i = 0; i < p_strVin.Length; i++)
{
strValue += replaceValues[p_strVin[i]].ToString();
}
string strFullPrefix = strWMI + strValue.Substring(3, 8) + strValue.Substring(9, 2) + strWMI_Ext;
return strFullPrefix;
}
}