From 6bd87416abaee02534d37a2f9337bb6b96bbaa32 Mon Sep 17 00:00:00 2001 From: Warren White Date: Wed, 30 Nov 2022 12:10:22 -0800 Subject: [PATCH 1/6] 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. --- src/additional/vinUS.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/additional/vinUS.js b/src/additional/vinUS.js index 15460d725..9e36fd3cc 100644 --- a/src/additional/vinUS.js +++ b/src/additional/vinUS.js @@ -25,6 +25,7 @@ $.validator.addMethod( "vinUS", function( v ) { f = FL[ i ]; d = v.slice( i, i + 1 ); if ( i === 8 ) { + // check digit vin is an integer cdv = d; } if ( !isNaN( d ) ) { @@ -35,6 +36,7 @@ $.validator.addMethod( "vinUS", function( v ) { d = VL[ n ]; d *= f; if ( isNaN( cdv ) && n === 8 ) { + // check digit vin is a string cdv = LL[ n ]; } break; @@ -43,11 +45,15 @@ $.validator.addMethod( "vinUS", function( v ) { } rs += d; } - cd = rs % 11; + + // check digit is an integer + cd = rs % 11; if ( cd === 10 ) { - cd = "X"; + // check digit is a string + cd = "X"; } - if ( cd === cdv ) { + // cd and cdv can be either integer string + if ( cd == cdv ) { return true; } return false; From af291cbc5f86b3fb5bd042c38d2a1c7656c9ec4d Mon Sep 17 00:00:00 2001 From: Warren White Date: Wed, 30 Nov 2022 15:26:43 -0800 Subject: [PATCH 2/6] Rewrote forloop, removed nested forloop --- src/additional/vinUS.js | 74 +++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 43 deletions(-) diff --git a/src/additional/vinUS.js b/src/additional/vinUS.js index 9e36fd3cc..88ea486ca 100644 --- a/src/additional/vinUS.js +++ b/src/additional/vinUS.js @@ -11,50 +11,38 @@ * @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 ) { - // check digit vin is an integer - 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 ) { - // check digit vin is a string - cdv = LL[ n ]; - } - break; - } - } - } - rs += d; - } - - // check digit is an integer + 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); + } + if (i === 8 ) + { + cdv = n; + if(d === 'X') { + cdv = 10; + } + } + rs += n * f + } cd = rs % 11; - if ( cd === 10 ) { - // check digit is a string - cd = "X"; - } - // cd and cdv can be either integer string - if ( cd == cdv ) { - return true; - } - return false; + if ( cd === cdv ) { + return true; + } + return false; }, "The specified vehicle identification number (VIN) is invalid." ); From 14b3098dc5f44ac82c02a693e1d2a57f85fea22e Mon Sep 17 00:00:00 2001 From: Warren White Date: Wed, 30 Nov 2022 16:11:10 -0800 Subject: [PATCH 3/6] Additional: fixed spacing issues --- src/additional/vinUS.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/additional/vinUS.js b/src/additional/vinUS.js index 88ea486ca..3fd2d128b 100644 --- a/src/additional/vinUS.js +++ b/src/additional/vinUS.js @@ -24,21 +24,20 @@ $.validator.addMethod( "vinUS", function( v ) { for ( i = 0; i < 17; i++ ) { f = FL[ i ]; d = v.slice( i, i + 1 ); - if( isNaN( d ) ) { + if ( isNaN( d ) ) { d = d.toUpperCase(); - n = VL[LL.indexOf(d)]; + n = VL[ LL.indexOf( d ) ]; + } else { + n = parseInt( d, 10 ); } - else { - n = parseInt(d); - } - if (i === 8 ) + if ( i === 8 ) { cdv = n; - if(d === 'X') { + if ( d === "X" ) { cdv = 10; } } - rs += n * f + rs += n * f; } cd = rs % 11; if ( cd === cdv ) { From 18cd39a819e451cc2eb15b786bf4d05f36fd0f4a Mon Sep 17 00:00:00 2001 From: Warren White Date: Wed, 30 Nov 2022 16:15:27 -0800 Subject: [PATCH 4/6] Additional: Add vinUS.js validation test cases Test cases include default test with 17 one's, and additional US and Canada VIN --- test/additional/vinUS.js | 9 +++++++++ test/index.html | 1 + 2 files changed, 10 insertions(+) create mode 100644 test/additional/vinUS.js diff --git a/test/additional/vinUS.js b/test/additional/vinUS.js new file mode 100644 index 000000000..7a6788d6d --- /dev/null +++ b/test/additional/vinUS.js @@ -0,0 +1,9 @@ +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( "2FTHF26F8" ), "InValid VIN number" ); + assert.ok( !method( "11111111X1111111" ), "Invalid test VIN license number" ); + assert.ok( !method( "1111111101111111" ), "Invalid test VIN license number" ); +} ); 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 @@ + From a51e61c162ed2ec1e691ef322b3663dd50c5c298 Mon Sep 17 00:00:00 2001 From: Warren White Date: Wed, 30 Nov 2022 16:21:28 -0800 Subject: [PATCH 5/6] Additional: add two more test casses for vinUS --- test/additional/vinUS.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/additional/vinUS.js b/test/additional/vinUS.js index 7a6788d6d..417318fae 100644 --- a/test/additional/vinUS.js +++ b/test/additional/vinUS.js @@ -3,6 +3,8 @@ QUnit.test( "vinUS", function( assert ) { 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 license number" ); assert.ok( !method( "1111111101111111" ), "Invalid test VIN license number" ); From 278ff26399485f630b286f4f6bf9e3eb23cd25a1 Mon Sep 17 00:00:00 2001 From: Warren White Date: Wed, 30 Nov 2022 16:31:23 -0800 Subject: [PATCH 6/6] Additional: removed text license number, should be VIN --- test/additional/vinUS.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/additional/vinUS.js b/test/additional/vinUS.js index 417318fae..3311b0111 100644 --- a/test/additional/vinUS.js +++ b/test/additional/vinUS.js @@ -6,6 +6,6 @@ QUnit.test( "vinUS", function( assert ) { 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 license number" ); - assert.ok( !method( "1111111101111111" ), "Invalid test VIN license number" ); + assert.ok( !method( "11111111X1111111" ), "Invalid test VIN" ); + assert.ok( !method( "1111111101111111" ), "Invalid test VIN" ); } ); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

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:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy