Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate a VIN number?

How can I validate a Vehicle Identification Number with PHP? I just need to check if the entered VIN number is correct or not.

like image 246
Optimus Avatar asked Sep 30 '10 14:09

Optimus


People also ask

Can a VIN number be faked?

Yes and no. A vehicle identification number is unique and exclusive to one vehicle. However, as technology advances, thieves are increasingly using car cloning. Criminals find an identical match for a stolen car – the same brand, model, and color – and counterfeit the identity of the original vehicle.

How do I check a VIN number for free?

You can get a free VIN check at the National Insurance Crime Bureau (NICB), VehicleHistory.com or iSeeCars.com/VIN. Just pop in your car's digits and these sites will do the VIN lookup and give you information on the vehicle.

Why is it saying my VIN number is invalid?

You may see an "invalid VIN" message for one of these reasons: 1) You may have mistyped the VIN. Verify the VIN you are entering is correct. You can find the VIN on your title/registration documents or get the VIN directly from the vehicle's dashboard or driver's side door jamb.

Can I get my vehicle specs by VIN number?

Identifying vehicle specs by VIN is as simple as grabbing a VIN number and plugging it into a VIN decoder tool. Some VIN decoding services, typically those designed for commercial use, allow users to submit the VIN pattern (characters 1-8, 10, and 11) as well as the full 17-digit VIN.


3 Answers

Here's something I wrote up real quick using the example in the wikipedia article.

Not guaranteed perfect or bug free or super efficient, but should provide you with a solid starting point:

Note: I included the edits provided by Confluence below, making the procedure slightly more succinct.

function validate_vin($vin) {

    $vin = strtolower($vin);
    if (!preg_match('/^[^\Wioq]{17}$/', $vin)) { 
        return false; 
    }

    $weights = array(8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2);

    $transliterations = array(
        "a" => 1, "b" => 2, "c" => 3, "d" => 4,
        "e" => 5, "f" => 6, "g" => 7, "h" => 8,
        "j" => 1, "k" => 2, "l" => 3, "m" => 4,
        "n" => 5, "p" => 7, "r" => 9, "s" => 2,
        "t" => 3, "u" => 4, "v" => 5, "w" => 6,
        "x" => 7, "y" => 8, "z" => 9
    );

    $sum = 0;

    for($i = 0 ; $i < strlen($vin) ; $i++ ) { // loop through characters of VIN
        // add transliterations * weight of their positions to get the sum
        if(!is_numeric($vin{$i})) {
            $sum += $transliterations[$vin{$i}] * $weights[$i];
        } else {
            $sum += $vin{$i} * $weights[$i];
        }
    }

    // find checkdigit by taking the mod of the sum

    $checkdigit = $sum % 11;

    if($checkdigit == 10) { // checkdigit of 10 is represented by "X"
        $checkdigit = "x";
    }

    return ($checkdigit == $vin{8});
}

Note: there is a small percent error with verifying VINs because of the nature of the checksum:

...a match does not prove the VIN is correct, because there is still a 1 in 11 chance of any two distinct VINs having a matching check digit.

Also note: 11111111111111111 will validate against the procedure above. Whether or not you want to check for that is up to you:

Straight-ones (seventeen consecutive '1's) will suffice the check-digit. This is because a value of one, multiplied against 89 (sum of weights), is still 89. And 89 % 11 is 1, the check digit. This is an easy way to test a VIN-check algorithm.

reference: http://en.wikipedia.org/wiki/Vehicle_identification_number#Check_digit_calculation

like image 57
bimbom22 Avatar answered Oct 24 '22 08:10

bimbom22


Here's a version of the code by jordan ported to Javascript, hope it's helpful to someone...

function validate_vin(vin)
{
  function isnumeric(mixed_var) {
    return (typeof(mixed_var) === 'number' || typeof(mixed_var) === 'string') && mixed_var !== '' && !isNaN(mixed_var);
  }
  var pattern = /^[^\Wioq]{17}$/;
  var weights = Array(8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2);
  var transliterations = {
    "a" : 1, "b" : 2, "c" : 3, "d" : 4,
    "e" : 5, "f" : 6, "g" : 7, "h" : 8,
    "j" : 1, "k" : 2, "l" : 3, "m" : 4,
    "n" : 5, "p" : 7, "r" : 9, "s" : 2,
    "t" : 3, "u" : 4, "v" : 5, "w" : 6,
    "x" : 7, "y" : 8, "z" : 9
  };

  vin = vin.toLowerCase();
  if(!vin.match(pattern)) { return false; }

  var sum = 0;
  for(var i=0; i<vin.length; i++) {
    if(!isnumeric(vin.charAt(i))) {
      sum += transliterations[vin.charAt(i)] * weights[i];
    } else {
      sum += parseInt(vin.charAt(i)) * weights[i];
    }  
  }

  var checkdigit = sum % 11;
  if(checkdigit == 10) { // check digit of 10 represented by X
    checkdigit = 'x';
  }

  return (checkdigit == vin.charAt(8));
}
like image 7
kyle Avatar answered Oct 24 '22 08:10

kyle


It's "VIN." "VIN Number" = "Vehicle Identification Number Number," which doesn't make sense.

You can see a definition of the structure of VINs here:
http://en.wikipedia.org/wiki/Vehicle_identification_number

And you can work from there, or you can grab this script here:
http://www.geekpedia.com/code29_Check-if-VIN-number-is-valid.html


Here is an improved version of the function posted by jordan:

$vin = "1M8GDM9AXKP042788";

function validate_vin($vin) {

    $vin = strtolower($vin);
    if (!preg_match('/^[^\Wioq]{17}$/', $vin)) { 
        return false; 
    }

    $weights = array(8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2);

    $transliterations = array(
        "a" => 1, "b" => 2, "c" => 3, "d" => 4,
        "e" => 5, "f" => 6, "g" => 7, "h" => 8,
        "j" => 1, "k" => 2, "l" => 3, "m" => 4,
        "n" => 5, "p" => 7, "r" => 9, "s" => 2,
        "t" => 3, "u" => 4, "v" => 5, "w" => 6,
        "x" => 7, "y" => 8, "z" => 9
    );

    $sum = 0;

    for($i = 0 ; $i < strlen($vin) ; $i++ ) { // loop through characters of VIN
        // add transliterations * weight of their positions to get the sum
        if(!is_numeric($vin{$i})) {
            $sum += $transliterations[$vin{$i}] * $weights[$i];
        } else {
            $sum += $vin{$i} * $weights[$i];
        }
    }

    // find checkdigit by taking the mod of the sum

    $checkdigit = $sum % 11;

    if($checkdigit == 10) { // checkdigit of 10 is represented by "X"
        $checkdigit = "x";
    }

    return ($checkdigit == $vin{8});
}
like image 3
Confluence Avatar answered Oct 24 '22 10:10

Confluence