Well this isn`t php, is a JavaScript code that validates a car chassis number (VIN). I expect comments on improving the code, as JavaScript isn`t my strong point, I know just as much as any php web developer should know.
var vinletters=new Array();
vinletters[1]="AJ";
vinletters[2]="BKS";
vinletters[3]="CLT";
vinletters[4]="DMU";
vinletters[5]="ENV";
vinletters[6]="FW";
vinletters[7]="GPX";
vinletters[8]="HY";
vinletters[9]="RZ";
//--------------12345678901234567
var vinweight= "8765432T098765432";
function valvin(serie){
if(serie.length == 17){
suma = 0;
for (i=0 ; i < serie.length; i++) {
nextchar = serie.charAt(i);
for (j in vinletters){
if(vinletters[j].indexOf(nextchar) != -1){
nextchar = j;
}
}
val = parseInt(nextchar);
weight = vinweight.charAt(i);
if(weight == "T"){
weight = 10;
}else{
weight = parseInt(weight);
}
suma += val * weight;
}
controlchar = suma % 11;
if(controlchar == 10) controlchar = "X";
if(controlchar == serie.charAt(8)){
return true;
}else{
return false;
}
}else{
return false;
}
return false;
}
by soroiu cristian
31 Oct 2009 at 21:30
ce mai faci omule? am dat de tine pe internet de vreo 2 saptamani si am tot cautat sa vad o adresa de mail, ceva. ar trebui sa iti pui pe site asa ceva, sau probabil ca nu am vazut eu.
by Thanks
03 Jun 2010 at 17:28
Thanks for your javascript.
I used what you did and converted into Plain Old Java.
Here is my solution for VIN Number Verification Validation:
public static boolean validateVIN (String vinNumber){ /* http://www.vinquery.com/faq.aspx What does Checksum Test do? Firstly, find the numerical value associated with each letter in the VIN. (I, O and Q are not allowed.) Digits use their own values. 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 Secondly, look up the weight factor for each position in the VIN except the 9th (the position of the check digit). 1st->8 2nd->7 3rd->6 4th->5 5th->4 6th->3 7th->2 8th->10 10th->9 11th->8 12th->7 13th->6 14th->5 15th->4 16th->3 17th->2 Thirdly, multiply the numbers and the numerical values of the letters by their assigned weight factor, and sum the resulting products. Divide the sum of the products by 11. The remainder is the calculated check digit. If the remainder is 10, the calculated check digit is the letter X. Finally, if the calculated check digit did match the 9th digit of the VIN entered by a user, the VIN passed the checksum test. It failed the checksum test otherwise. */ // Sample VIN: 1FTHS24HXTHA20979 // Sample VIN: 2R9CE162931625191 // Sample VIN: 1GTEC19VX2Z261054 // Sample VIN: 2GTEK19R5V1558932 // Sample VIN: 1B7KF2366XJ593564 // Sample VIN: 3D7LU38C54G256868 // Sample VIN: 1FTSW31PX4EB59668 boolean vinIsValid = false; if(vinNumber.length() == 17) { String nextchar, weight, controlchar; int val; String[] vinletters=new String[9]; vinletters[0]="AJ"; vinletters[1]="BKS"; vinletters[2]="CLT"; vinletters[3]="DMU"; vinletters[4]="ENV"; vinletters[5]="FW"; vinletters[6]="GPX"; vinletters[7]="HY"; vinletters[8]="RZ"; //---------------------12345678901234567 String posMultiplier= "8765432T098765432"; int checksum = 0; for (int i=0 ; i < vinNumber.length(); i++) { nextchar = Character.toString(vinNumber.charAt(i)); for (int j =0; j<vinletters.length; j++){ if(vinletters[j].indexOf(nextchar) != -1){ nextchar = Integer.toString(j + 1); } } val = Integer.parseInt(nextchar); char multiplier = posMultiplier.charAt(i); weight = Character.toString(posMultiplier.charAt(i)); if(weight.equals("T")){ weight = "10"; } checksum += val * Integer.parseInt(weight); } controlchar = Integer.toString(checksum % 11); if(controlchar.equals("10")) controlchar = "X"; if(controlchar.equals( Character.toString(vinNumber.charAt(8)) )){ vinIsValid = true; }else{ vinIsValid = false; } } else { vinIsValid = false; } return vinIsValid; }by Lucian Marica
03 Jun 2010 at 19:40
I`m so glad that someone found this useful :)