Skip to content

Instantly share code, notes, and snippets.

@josiahwiebe
Created May 8, 2020 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josiahwiebe/c121a64a47352b14b0d881c7af77fce1 to your computer and use it in GitHub Desktop.
Save josiahwiebe/c121a64a47352b14b0d881c7af77fce1 to your computer and use it in GitHub Desktop.
Validate VIN function
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