Kevin Brown
Kevin Brown

Reputation: 12650

javascript break down VIN number

I want to use javascript to break down the VIN in an array, probably using a regex and then some kind of loop...

Here's how to read VIN's:

http://forum.cardekho.com/topic/600-decoding-vehicle-identification-number-vin/

I'm really asking about best practice, not a copy+paste solution...I can probably get the regex groups, but after that, how do I use those values to, for example, say first digit is "5", that means USA. I want to output "USA" somewhere...is the best way to use some kind of case loop?

Thanks for the advice!

Upvotes: 0

Views: 298

Answers (1)

Ehdrian
Ehdrian

Reputation: 81

You can simply split up the string and plug it into an associative array.

var origin = { '5': 'USA', "6": 'Other Country' };
var fuel= { 'J': 'GAS', "D": 'Other' };

var VINString = "5 J";
var VINArray = VINString.split(' ');

document.write(origin[VINArray[0]]);
document.write(fuel[VINArray[1]]);

This will allow for keys of both letters (ie 'J') and numbers (ie '5')

I'm not sure why you would use regex.... just cut up the string and send the values directly into the associative array. The VIN is of pre-defined lengths and values.

Cheers

Upvotes: 1

Related Questions