ts版车辆VIN码的校验算法:
/*
* @Autor: Jason
* @Date: 2020-12-30 14:36:48
* @LastEditors: Jason
* @LastEditTime: 2020-12-30 14:42:54
* @FilePath: /src/utils/verify_vin.ts
* @description: vin校验
*/
//车架号验证 vin
export const verifyVin = (vin: string): boolean => {
if (vin.length > 0 && vin.length != 17) {
return false;
} else {
var vinVal = vin.toUpperCase();
var charToNum: { [key: string]: number } = {
A: 1,
B: 2,
C: 3,
D: 4,
E: 5,
F: 6,
G: 7,
H: 8,
J: 1,
K: 2,
L: 3,
M: 4,
N: 5,
P: 7,
R: 9,
S: 2,
T: 3,
U: 4,
V: 5,
W: 6,
X: 7,
Y: 8,
Z: 9,
};
var obj = 0;
var arr = new Array();
for (var i = 0; i < vinVal.length; i++) {
var temp = vinVal.charAt(i);
if (charToNum[temp]) {
arr[i] = charToNum[temp];
} else {
arr[i] = Number(temp);
}
if (i == 8) {
arr[i] = vinVal.charAt(i);
}
}
var a1 = 8;
for (var i = 0; i < 7; i++) {
obj += Number(arr[i]) * a1;
a1--;
}
obj += Number(arr[7]) * 10;
var a2 = 9;
for (var i = 9; i < 17; i++) {
obj += Number(arr[i]) * a2;
a2--;
}
var result: number | string = Number(obj) % 11;
if (parseInt(result.toString()) === 10) {
result = 'X';
}
if (result == arr[8]) {
//成功
return true;
} else {
//失败
return false;
}
}
};
参考文献1: js车架号检测(vin)
参考文献2: 车辆VIN码的校验算法