8000 Additional: vinUS validation fails on valid vin numbers by wewhite · Pull Request #2460 · jquery-validation/jquery-validation · GitHub
[go: up one dir, main page]

Skip to content

Additional: vinUS validation fails on valid vin numbers #2460

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 31 additions & 38 deletions src/additional/vinUS.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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