Blame view

frontend/web/js/validation/src/additional/vinUS.js 1.29 KB
18b850c7   Administrator   Importers CRUD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  /**
   * Return true, if the value is a valid vehicle identification number (VIN).
   *
   * Works with all kind of text inputs.
   *
   * @example <input type="text" size="20" name="VehicleID" class="{required:true,vinUS:true}" />
   * @desc Declares a required input element whose value must be a valid vehicle identification number.
   *
   * @name $.validator.methods.vinUS
   * @type Boolean
   * @cat Plugins/Validate/Methods
   */
  $.validator.addMethod("vinUS", function(v) {
  	if (v.length !== 17) {
  		return false;
  	}
  
  	var LL = [ "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" ],
  		VL = [ 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 7, 9, 2, 3, 4, 5, 6, 7, 8, 9 ],
  		FL = [ 8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2 ],
  		rs = 0,
  		i, n, d, f, cd, cdv;
  
  	for (i = 0; i < 17; i++) {
  		f = FL[i];
  		d = v.slice(i, i + 1);
  		if (i === 8) {
  			cdv = d;
  		}
  		if (!isNaN(d)) {
  			d *= f;
  		} else {
  			for (n = 0; n < LL.length; n++) {
  				if (d.toUpperCase() === LL[n]) {
  					d = VL[n];
  					d *= f;
  					if (isNaN(cdv) && n === 8) {
  						cdv = LL[n];
  					}
  					break;
  				}
  			}
  		}
  		rs += d;
  	}
  	cd = rs % 11;
  	if (cd === 10) {
  		cd = "X";
  	}
  	if (cd === cdv) {
  		return true;
  	}
  	return false;
  }, "The specified vehicle identification number (VIN) is invalid.");