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> 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>