From 13b859e35735086cb320423b3585d8a6edee4349 Mon Sep 17 00:00:00 2001 From: Warren White <111083379+wewhite@users.noreply.github.com> Date: Thu, 1 Dec 2022 02:50:24 -0800 Subject: [PATCH] Additional: vinUS validation fails on valid vin numbers (#2460) * 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 --- src/additional/vinUS.js | 69 ++++++++++++++++++---------------------- test/additional/vinUS.js | 11 +++++++ test/index.html | 1 + 3 files changed, 43 insertions(+), 38 deletions(-) create mode 100644 test/additional/vinUS.js diff --git a/src/additional/vinUS.js b/src/additional/vinUS.js index 15460d725..3fd2d128b 100644 --- a/src/additional/vinUS.js +++ b/src/additional/vinUS.js @@ -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." ); diff --git a/test/additional/vinUS.js b/test/additional/vinUS.js new file mode 100644 index 000000000..3311b0111 --- /dev/null +++ b/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" ); +} ); diff --git a/test/index.html b/test/index.html index bf22a640a..17c6438b8 100644 --- a/test/index.html +++ b/test/index.html @@ -19,6 +19,7 @@ +