Skip to content

Commit

Permalink
Additional: vinUS validation fails on valid vin numbers (#2460)
Browse files Browse the repository at this point in the history
* Removed === compare, changed to ==
Compare by value and type (===) does not work for this algorithm, as both cd and cdv can be either types at the same time.
By comparing by value only (==) cd and cdv can be either integer or string, as a string number will be converted to a number reqardless of type.

* Rewrote forloop, removed nested forloop

* Additional: fixed spacing issues

* Additional: Add vinUS.js validation test cases

Test cases include default test with 17 one's, and additional US and Canada VIN

* Additional: add two more test casses for vinUS

* Additional: removed text license number, should be VIN
  • Loading branch information
wewhite committed Dec 1, 2022
1 parent 29fb609 commit 13b859e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 38 deletions.
69 changes: 31 additions & 38 deletions src/additional/vinUS.js
Expand Up @@ -11,44 +11,37 @@
* @cat Plugins/Validate/Methods
*/
$.validator.addMethod( "vinUS", function( v ) {
if ( v.length !== 17 ) {
return false;
}
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;
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;
for ( i = 0; i < 17; i++ ) {
f = FL[ i ];
d = v.slice( i, i + 1 );
if ( isNaN( d ) ) {
d = d.toUpperCase();
n = VL[ LL.indexOf( d ) ];
} else {
n = parseInt( d, 10 );
}
if ( i === 8 )
{
cdv = n;
if ( d === "X" ) {
cdv = 10;
}
}
rs += n * f;
}
cd = rs % 11;
if ( cd === cdv ) {
return true;
}
return false;
}, "The specified vehicle identification number (VIN) is invalid." );
11 changes: 11 additions & 0 deletions test/additional/vinUS.js
@@ -0,0 +1,11 @@
QUnit.test( "vinUS", function( assert ) {
var method = methodTest( "vinUS" );
assert.ok( method( "11111111111111111" ), "Valid test VIN number" );
assert.ok( method( "1FTFX1CT9CFD06231" ), "Valid US VIN number" );
assert.ok( method( "2FTHF26F8SCA68695" ), "Valid CAN VIN number" );
assert.ok( method( "LJCPCBLCX11000237" ), "Valid VIN with X check digit" );
assert.ok( !method( "LJCPCBLC011000237" ), "Invalid VIN with 0 check digit" );
assert.ok( !method( "2FTHF26F8" ), "InValid VIN number" );
assert.ok( !method( "11111111X1111111" ), "Invalid test VIN" );
assert.ok( !method( "1111111101111111" ), "Invalid test VIN" );
} );
1 change: 1 addition & 0 deletions test/index.html
Expand Up @@ -19,6 +19,7 @@
<script src="additional/creditcard.js"></script>
<script src="additional/netmask.js"></script>
<script src="additional/abaRoutingNumber.js"></script>
<script src="additional/vinUS.js"></script>
<script src="aria.js"></script>
<script src="error-placement.js"></script>
</head>
Expand Down

0 comments on commit 13b859e

Please sign in to comment.