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 @@ +
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: