A Regex Pattern to Validate a VIN Number: 17 Characters, No I, O, or Q
A regex pattern to validate a VIN number is one of those patterns that looks trivial and then quietly breaks in production. Every vehicle has a unique Vehicle Identification Number, a 17 character alphanumeric sequence, and the classic pattern from the regex101 library reads \b[(A-H|J-N|P|R-Z|0-9)]{17}\b in its community form, or in cleaner character class form, ^[A-HJ-NPR-Z0-9]{17}$. The second form is the one I would ship. The rest of this article is about why the first form misleads, what the pattern cannot do, and the check digit detail that turns a shape check into something resembling validation.
Start with the rules of the format itself, because the pattern is just those rules written in regex syntax. A modern VIN is exactly 17 characters. It uses uppercase letters and digits, but never the letters I, O, or Q, because on stamped metal and in bad photos those letters are too easy to confuse with the digits 1 and 0. The character class A-HJ-NPR-Z is exactly that exclusion written out: A through H, skip I, J through N, skip O, P, skip Q, R through Z. Add 0-9, require 17 of them, and you have the whole format check in one line.
The two versions, and which job each one does
There are two patterns here, not one, and mixing them up is the most common VIN regex bug I see. For validating a form field, where the entire input must be a VIN and nothing else, anchor both ends:
^[A-HJ-NPR-Z0-9]{17}$
The ^ and $ mean the whole string, start to finish, must be 17 allowed characters. Without them, a 20 character string containing a 17 character run of allowed characters would match, and your form would accept garbage with a VIN-shaped middle.
For finding VINs inside larger text, like scanning a document or a log line, anchors are wrong, because the VIN is not the whole string. Use word boundaries instead:
\b[A-HJ-NPR-Z0-9]{17}\b
The \b markers require a boundary between a word character and a non-word character at each end, so the match cannot start or end mid-token. That is what the regex101 community pattern does, and it is the right tool for extraction. It is the wrong tool for validation, because a VIN pasted with a trailing space or a surrounding quote can behave unexpectedly depending on the engine. Pick the pattern for the job, and do not reuse one for the other.
One more variant worth mentioning: the community pattern writes the class with pipes and parentheses, [(A-H|J-N|P|R-Z|0-9)], which inside a character class treats the pipes and parentheses as literal characters. In practice most engines still match VINs with it, because the allowed letters and digits dominate, but it also technically permits pipe and parenthesis characters, which a VIN never contains. The clean class form avoids that entirely. If you copy a pattern from a library, read the class character by character before shipping it. Library patterns are starting points, not guarantees.
Test it with the example that catches the exclusion
The regex101 entry gives a perfect test pair, and I use it verbatim because it isolates exactly one rule. SALVA2AE4EH877322 is valid: 17 characters, all allowed. SALVO2AE4EH877322 is not, because it contains an O. One letter, same length, and the pattern must reject the second while accepting the first. If your pattern accepts both, your class includes O, usually because someone wrote A-Z out of habit instead of the exclusion ranges.
Build a small test set around that pair before you ship. Valid: the SALVA example above. Invalid: the SALVO example, a 16 character string, an 18 character string, a string with a lowercase letter if you are not normalizing, and a string with a space or hyphen in the middle. VINs are sometimes printed with spaces or dashes for readability, so decide your policy deliberately: strip separators before matching, or reject them. Either is defensible. Accepting them silently inside the pattern, by adding space and hyphen to the class, is not, because it changes what counts as 17 characters and lets malformed input through.
Lowercase deserves its own decision too. VINs are uppercase by standard. My recommendation is to uppercase the input first, then run the uppercase pattern. That normalizes user input at the boundary, where normalization belongs, instead of widening the pattern to accept lowercase and then storing mixed-case VINs that will not match a database lookup later. A lookup that fails because of case is a bug you will chase for a week.
What the pattern cannot do: the check digit
Here is the delayed reveal, and it is the part that matters most. A regex checks shape. It cannot check whether a VIN was ever assigned to a real vehicle, and it cannot even check whether the 17 characters are internally consistent. That second job belongs to the check digit, the character at position 9, which is calculated from the other 16 characters through a weighted transliteration. A string can be 17 allowed characters long and still fail the check digit, which means at least one character is wrong, a typo, a misread, a transposition.
I am not going to pretend a regex can do that calculation. It cannot, not sanely. The honest architecture is two layers: the regex as the fast first gate, rejecting wrong lengths and illegal letters with a clear error message, and then a check digit function in real code for anything that passes the gate. If the check digit fails, the message should say the VIN looks mistyped, not that it is invalid in some abstract sense, because a failed check digit almost always means a transcription error, and the user can fix it by comparing against the plate or the document.
And even a VIN that passes both layers is only plausible, not proven. Proving it means a lookup against a vehicle database or a manufacturer record. For a form that collects a VIN for a quote or a registration, the two layers are usually enough, because the cost of a wrong VIN is a follow-up email. For a title transfer, an insurance binding, or a safety recall, a lookup is the only validation that counts. Match the rigor to the stakes, and say which layer you implemented, so the next person reading your code does not mistake a shape check for proof.
Frequently asked questions
What is the regex pattern to validate a VIN number?
For a full-string match: ^[A-HJ-NPR-Z0-9]{17}$. It requires exactly 17 characters from uppercase letters excluding I, O, and Q, plus digits. For finding VINs inside larger text, use \b[A-HJ-NPR-Z0-9]{17}\b instead of the anchors.
Why are I, O, and Q excluded from VINs?
To avoid confusion with the digits 1 and 0. A VIN stamped on metal or read from a photo is easy to misread, so the standard leaves those three letters out entirely.
Does a regex check prove a VIN is real?
No. A regex checks shape only: length and allowed characters. A 17 character string can pass the pattern and still be a VIN that was never assigned. Real validation adds the check digit calculation at position 9, and ultimately a lookup against a vehicle database.
Should a VIN regex accept lowercase letters?
VINs are uppercase by standard, so the cleanest approach is to uppercase the input before matching, then run the uppercase pattern. Accepting lowercase inside the pattern hides input problems instead of normalizing them.
Why does my VIN regex match inside longer strings?
Because the pattern has no anchors. Without ^ and $ for a full-field check, or \b word boundaries for extraction, a 17 character run inside a longer string will match. Anchor the pattern to the job: full validation gets ^ and $, extraction gets \b on both sides.
Related reading: Regex to Validate a Social Security Number · Regex URL Validation Mistakes · Regex Word Boundaries Explained · Regex for Credit Card Number Validation
One practical regex guide a week. Get it by email.