JavaScript / jQuery VIN Validator

Has anyone ever created a VIN validator? I am trying to create a text box in which the user will enter the vehicle identification number, and then JS / jQuery will check if it is correct or not if they are mistakenly mistaken.

I am very new to JS / jQuery and found some examples, but of course I couldn’t get them to work correctly ... Anyone who has any ideas or suggestions would be very grateful, especially if you can tell me how to configure what i have to work correctly!

Note. isVin () function provided by cflib.org

HTML:

<label name="vin">VIN</label> <input type="text" name="vin" /> 

ColdFusion:

 <cfscript> /** * US Vehicle Identification Number (VIN) validation. * version 1.0 by Christopher Jordan * version 1.1 by RHPT, Peter Boughton &amp; Adam Cameron (original function rejected valid VINs) * * @param v VIN to validate (Required) * @return Returns a boolean. * @author Christopher Jordan ( cjordan@placs.net ) * @version 1, February 19, 2013 */ function isVIN(v) { var i = ""; var d = ""; var checkDigit = ""; var sum = 0; var weights = [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 }; var vinRegex = "(?x) ## allow comments ^ ## from the start of the string ## see http://en.wikipedia.org/wiki/Vehicle_Identification_Number for VIN spec [AZ\d]{3} ## World Manufacturer Identifier (WMI) [AZ\d]{5} ## Vehicle decription section (VDS) [\dX] ## Check digit [AZ\d] ## Model year [AZ\d] ## Plant \d{6} ## Sequence $ ## to the end of the string "; if (! REFindNoCase(vinRegex, arguments.v)) { return false; } for (i = 1; i <= len(arguments.v); i++) { d = mid(arguments.v, i, 1); if (! isNumeric(d)) { sum += transliterations[d] * weights[i]; } else { sum += d * weights[i]; } } checkDigit = sum % 11; if (checkDigit == 10) { checkDigit = "x"; } return checkDigit == mid(arguments.v, 9, 1); } </cfscript> 

Test code:

  <cfoutput> <cfset vin = "1GNDM19ZXRB170064"> #vin#: #isVin(vin)#<br /> <cfset vin = "1FAFP40634F172825"> #vin#: #isVin(vin)#<br /> </cfoutput> 
+5
source share
2 answers

Here's a client solution using regular expressions.

 $(function() { $("#vin").on("keyup blur", function() { if (validateVin($("#vin").val())) $("#result").html("That a VIN"); else $("#result").html("Not a VIN"); }); }); 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> 
+8
source

If you decode VIN for cars sold in North America 1 you can confirm the following:

  • What VIN is 17 digits in length
  • If 17 digits, correct check digit (9th position)

As you check the VIN in real time, when the user enters it, it is important to be effective. This means that with quick user input, it does not work fast. Although you can use a regular expression for this task, it is much slower than just checking the length of the VIN. Once the VIN is 17 digits long, you can check the check digit (which, by the way, is also faster than the regular expression itself), see below).

Regular expressions are not suitable for VIN checking for several reasons:

  • Performance

    . This is not as fast as simply checking the length, and the trade-off for speed does not give you a precise improvement in accuracy because you cannot check the check digit.

  • Accuracy

    . While this is true, you can ensure that the VIN matches the correct pattern in terms of length and valid characters, which is not enough. KNDJB723025140702 is a valid VIN, but KNDJB723025140703 is not (the only difference is the last number). A regular solution will show that both VINs are valid and the second is false positive and therefore incorrect.

Wikipedia has a really good script for validating a check digit, which also completes quickly, only confirming the check digit if the VIN is 17 digits.

In my own testing, the script below is a lot, you need faster (about 40x) than using regex to check the VIN, and it has the added benefit of getting 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.PR.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 usually do not use a check digit, and for some vehicles there may even be 18 digits in length (Mercedes).

+7
source

Source: https://habr.com/ru/post/1204830/


All Articles