Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VIN Validation RegEx

I have written a VIN validation RegEx based on the http://en.wikipedia.org/wiki/Vehicle_identification_number but then when I try to run some tests it is not accepting some valid VIN Numbers.

My RegEx:

^[A-HJ-NPR-Za-hj-npr-z\\d]{8}[\\dX][A-HJ-NPR-Za-hj-npr-z\\d]{2}\\d{6}$

VIN Number Not Working:
1ftfw1et4bfc45903
WP0ZZZ99ZTS392124

VIN Numbers Working:
19uya31581l000000
1hwa31aa5ae006086

(I think the problem occurs with the numbers at the end, Wikipedia made it sound like it would end with only 6 numbers and the one that is not working but is a valid number only ends with 5)

Any Help Correcting this issue would be greatly appreciated!

like image 604
Vicki Avatar asked May 19 '15 00:05

Vicki


People also ask

What is RegEx in validation?

What is RegEx Validation (Regular Expression)? RegEx validation is essentially a syntax check which makes it possible to see whether an email address is spelled correctly, has no spaces, commas, and all the @s, dots and domain extensions are in the right place.

What VIN number means?

The car's vehicle identification number (VIN) is the identifying code for a SPECIFIC automobile. The VIN serves as the car's fingerprint, as no two vehicles in operation have the same VIN. A VIN is composed of 17 characters (digits and capital letters) that act as a unique identifier for the vehicle.


Video Answer


3 Answers

I can't help you with a perfect regex for VIN numbers -- but I can explain why this one is failing in your example of 1ftfw1et4bfc45903:

^[A-HJ-NPR-Za-hj-npr-z\d]{8}[\dX][A-HJ-NPR-Za-hj-npr-z\d]{2}\d{6}$

Explanation:

  • ^[A-HJ-NPR-Za-hj-npr-z\d]{8}
    This allows for 8 characters, composed of any digits and any letters except I, O, and Q; it properly finds the first 8 characters:
    1ftfw1et
  • [\dX]
    This allows for 1 character, either a digit or a capital X; it properly finds the next character:
    4
  • [A-HJ-NPR-Za-hj-npr-z\d]{2}
    This allows for 2 characters, composed of any digits and any letters except I, O, and Q; it properly finds the next 2 characters:
    bf
  • \d{6}$
    This allows for exactly 6 digits, and is the reason the regex fails; because the final 6 characters are not all digits:
    c45903
like image 63
Joe DeRose Avatar answered Oct 12 '22 04:10

Joe DeRose


Dan is correct - VINs have a checksum. You can't utilize that in regex, so the best you can do with regex is casting too wide of a net. By that I mean that your regex will accept all valid VINs, and also around a trillion (rough estimate) non-VIN 17-character strings.

If you are working in a language with named capture groups, you can extract that data as well.

So, if your goal is:

  • Only to not reject valid VINs (letting in invalid ones is ok) then use Fransisco's answer, [A-HJ-NPR-Z0-9]{17}.

  • Not reject valid VINs, and grab info like model year, plant code, etc, then use this (note, you must use a language that can support named capture groups - off the top of my head: Perl, Python, Elixir, almost certainly others but not JS): /^(?<wmi>[A-HJ-NPR-Z\d]{3})(?<vds>[A-HJ-NPR-Z\d]{5})(?<check>[\dX])(?<vis>(?<year>[A-HJ-NPR-Z\d])(?<plant>[A-HJ-NPR-Z\d])(?<seq>[A-HJ-NPR-Z\d]{6}))$/ where the names are defined at the end of this answer.

  • Not reject valid VINs, and prevent some but not all invalid VINs, you can get specific like Pedro does.

  • Only accept valid VINs: you need to write code (just kidding, GitHub exists).

Capture group name key:

  • wmi - World manufacturer identifier
  • vds - Vehicle descriptor section
  • check - Check digit
  • vis - Vehicle identifier section
  • year - Model year
  • plant - Plant code
  • seq - Production sequence number
like image 45
Sam H. Avatar answered Oct 12 '22 06:10

Sam H.


This regular expression is working fine for validating US VINs, including the one you described:

[A-HJ-NPR-Z0-9]{17}

Remember to make it case insensitive with flag i

Source: https://github.com/rfink/angular-vin

like image 9
Francisco Goldenstein Avatar answered Oct 12 '22 06:10

Francisco Goldenstein