Created
May 8, 2020 16:29
-
-
Save josiahwiebe/c121a64a47352b14b0d881c7af77fce1 to your computer and use it in GitHub Desktop.
Validate VIN function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const validateVin = value => { | |
if (value.length !== 17) return false | |
let chars = 'ABCDEFGHJKLMNPRSTUVWXYZ0123456789' | |
let nValue = new Array( | |
1, | |
2, | |
3, | |
4, | |
5, | |
6, | |
7, | |
8, | |
1, | |
2, | |
3, | |
4, | |
5, | |
7, | |
9, | |
2, | |
3, | |
4, | |
5, | |
6, | |
7, | |
8, | |
9, | |
0, | |
1, | |
2, | |
3, | |
4, | |
5, | |
6, | |
7, | |
8, | |
9 | |
) | |
let nWeight = new Array(8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2) | |
let nAccum = 0 | |
for (let i = 0; i < 17; i++) { | |
if (i !== 8) { | |
nAccum += nValue[chars.indexOf(value[i])] * nWeight[i] | |
} | |
} | |
let cCheckDigit | |
let nCheckDigit = nAccum % 11 | |
if (nCheckDigit === 10) { | |
cCheckDigit = 'X' | |
} else { | |
cCheckDigit = nCheckDigit.toString() | |
} | |
if (value[8] !== cCheckDigit) { | |
return false | |
} else { | |
return true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment