Has anyone ever created a VIN Validator? I am trying to create a textbox where the user will enter in a Vehicle Identification Number and then JS/jQuery will validate if its
Here's a client-side solution using Regular Expressions.
$(function() {
$("#vin").on("keyup blur", function() {
if (validateVin($("#vin").val()))
$("#result").html("That's a VIN");
else
$("#result").html("Not a VIN");
}).trigger("blur");
});
function validateVin(vin) {
var re = new RegExp("^[A-HJ-NPR-Z\\d]{8}[\\dX][A-HJ-NPR-Z\\d]{2}\\d{6}$");
return vin.match(re);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<label name="vin">VIN</label>
<input type="text" id="vin" value="1FAFP40634F172825" />
<span id="result"></span>
If you're decoding VINs for vehicles sold in North America1, you can validate the following:
As you'll be checking the VIN in real time as the user types it in, it's important to be efficient. This means failing fast while the user is typing it in. While you can use regex for this task, it's far slower than simply checking the VIN's length. Once the VIN is 17 digits long then you can validate the check digit (which, incidentally, is itself also faster than regex - see below).
Regular expressions are not a good fit for VIN validation for a couple reasons:
Performance. It's not as fast as simply checking the length, and the trade-off for speed doesn't net you that much of an improvement in accuracy because you can't validate the check digit.
Accuracy. While it's true you can ensure a VIN meets the correct pattern in terms of length and valid characters, that is not enough. KNDJB723025140702
is a valid VIN while KNDJB723025140703
is not (the only difference is the last number). A regex solution will show both VINs as being valid, with the second being a false-positive and therefore incorrect.
Wikipedia has a really good script for performing check digit validation that also fails quickly by only validating the check digit if the VIN is 17 digits.
In my own testing, the script below is much, must faster (roughly 40x) than using regex to validate a VIN, and it has the added benefit of returning accurate results.
$(function() {
var $ctrl = $('#vin'),
$msg = $('#validation-message');
$ctrl.keyup(function() {
if (validateVin($ctrl.val())) {
$msg.html("VIN is valid!");
} else {
$msg.html("VIN is not valid");
}
});
});
function validateVin(vin) {
return validate(vin);
// source: https://en.wikipedia.org/wiki/Vehicle_identification_number#Example_Code
// ---------------------------------------------------------------------------------
function transliterate(c) {
return '0123456789.ABCDEFGH..JKLMN.P.R..STUVWXYZ'.indexOf(c) % 10;
}
function get_check_digit(vin) {
var map = '0123456789X';
var weights = '8765432X098765432';
var sum = 0;
for (var i = 0; i < 17; ++i)
sum += transliterate(vin[i]) * map.indexOf(weights[i]);
return map[sum % 11];
}
function validate(vin) {
if (vin.length !== 17) return false;
return get_check_digit(vin) === vin[8];
}
// ---------------------------------------------------------------------------------
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
VIN:
<input id="vin" type="text" />
<span id="validation-message"></span>
[1] Vehicles sold in the EU don't typically use a check digit, and for some cars can even be 18 digits in length (Mercedes).