Check if VIN number is valid

A photo-realistic illustration of a PHP codebook with pages open on a section about VIN (Vehicle Identification Number) validation.
Complex PHP code for checking the validity of VINs (vehicle identification numbers). It also returns the check digit of each VIN number.
1. <?php
2. $VinNum = "WDBVG78J38A002076";
3. if(strlen($VinNum) != "17")
4. {
5.         echo "The number of characters you have entered (".strlen($VinNum).") does not match a valid VIN number.";
6.         exit;
7. }
8.  
9. $VinNum = strtoupper($VinNum);
10. $Model = array("A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "R", "S", "T", "V", "W", "X", "Y");
11. $Weight = array("8", "7", "6", "5", "4", "3", "2", "10", "9", "8", "7", "6", "5", "4", "3", "2");
12. $Char = array("A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
13. $CharVals = 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");
14. $VinChars = array();
15.  
16. $Counter = 0;
17. foreach ($Char as $CurrChar)
18. {
19.         $VinChars[$CurrChar] = $CharVals[$Counter];
20.         $Counter++;
21. }
22.  
23. $CheckDigits = array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "X");
24. $Counter = 0;
25. $VinArray = array();
26. for ($i = 0; $i < 17; $i++)
27. {
28.         if ($i!=8)
29.         {
30.                 $VinArray[$Counter] = substr($VinNum, $i, 1);
31.                 $Counter++;
32.         }
33. }
34.  
35. $Total = 0;
36. for($i = 0; $i < 16; $i++)
37. {
38.         $ThisDigit = $VinArray[$i];
39.         $ThisTotal = $Weight[$i] * $VinChars[$ThisDigit];
40.         $Total = $Total + $ThisTotal;
41. }
42.  
43. $Remainder = fmod($Total, 11);
44. if (substr($VinNum, 8, 1)!= $CheckDigits[$Remainder])
45. {
46.         echo "VIN number is not valid.<br />";
47. }
48. else
49. {
50.         echo "VIN number is valid.<br />";
51. }
52. echo "Computed check digit: ".$CheckDigits[$Remainder]."<br />";
53. echo "Your check digit: ".substr($VinNum, 8, 1);
54. ?>
Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top