算法根源:
大致是這樣車輛識別代碼的第9位是檢驗位,檢驗位可用0—9中任一數字或字母“X”表示。其它位置的數字和字母代表的意義個廠家含義可能不同,但在VIN碼的其他16位字碼確定後,按以下方法計算得出第九位的檢驗位。
首先將其它16位中的字母按下列關系轉換成數字:
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
每個位置都有個加權數:
位置:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
權數:8 7 6 5 4 3 2 10 * 9 8 7 6 5 4 3 2
最後將檢驗位之外的16位每一位的加權系數乘以此位的對應值,再將各乘積相加,求得的和除以11,所得的余數就是檢驗位的數值。如果余數為10,則檢驗位為字母“X”。
JS代碼:
復制代碼 代碼如下:
<script type="text/javascript">
var Arr = new Array();
var Brr = new Array();
Arr['A'] = 1;
Arr['B'] = 2;
Arr['C'] = 3;
Arr['D'] = 4;
Arr['E'] = 5;
Arr['F'] = 6;
Arr['G'] = 7;
Arr['H'] = 8;
Arr['J'] = 1;
Arr['K'] = 2;
Arr['L'] = 3;
Arr['M'] = 4;
Arr['N'] = 5;
Arr['P'] = 7;
Arr['R'] = 9;
Arr['S'] = 2;
Arr['T'] = 3;
Arr['U'] = 4;
Arr['V'] = 5;
Arr['W'] = 6;
Arr['X'] = 7;
Arr['Y'] = 8;
Arr['Z'] = 9;
Arr['1'] = 1;
Arr['2'] = 2;
Arr['3'] = 3;
Arr['4'] = 4;
Arr['5'] = 5;
Arr['6'] = 6;
Arr['7'] = 7;
Arr['8'] = 8;
Arr['9'] = 9;
Arr['0'] = 0;
Brr[1]=8;
Brr[2]=7;
Brr[3]=6;
Brr[4]=5;
Brr[5]=4;
Brr[6]=3;
Brr[7]=2;
Brr[8]=10;
Brr[9]=0;
Brr[10]=9;
Brr[11]=8;
Brr[12]=7;
Brr[13]=6;
Brr[14]=5;
Brr[15]=4;
Brr[16]=3;
Brr[17]=2;
function getCheckCode(sVIN)
{
var sKYZF="ABCDEFGHJKLMNPRSTUVWXYZ1234567890";
var sJYW ='';
var bl = false;
var blKYZF = false;
if (sVIN.length == 17)
{
var iJQS=0,intTemp=0;
ht = Arr;
htZM = Brr;
try
{
for (var i = 0; i <sVIN.length; i++)
{
if (sKYZF.indexOf(sVIN.substr(i, 1)) != -1)
{
blKYZF = true;
iJQS = iJQS + parseInt(ht[sVIN.substr(i, 1)]) * parseInt(htZM[(i + 1)]);
}
else
{
blKYZF = false;
break;
}
}
if (blKYZF)
{
intTemp = iJQS%11;
if (intTemp == 10)
{
sJYW = "X";
}
else
{
sJYW = intTemp.toString();
}
if (sJYW == sVIN.substr(8, 1)) bl = true;
}
else
{
bl = false;
}
}
catch(err)
{
bl = false;
}
}
return bl;
}
alert(getCheckCode("WVGAB97PXCD010692"));
</script>