Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glcheetham/75c3821e5ddcad7f65bdd18474a8b8a4 to your computer and use it in GitHub Desktop.
Save glcheetham/75c3821e5ddcad7f65bdd18474a8b8a4 to your computer and use it in GitHub Desktop.
Javascript Regular Expression to Validate UK Number Plates

Javascript Regular Expression to Validate UK Number Plates

Regular Expression

Works in jQuery, React, Angular, Javascript es6 and es5

(^[A-Z]{2}[0-9]{2}\s?[A-Z]{3}$)|(^[A-Z][0-9]{1,3}[A-Z]{3}$)|(^[A-Z]{3}[0-9]{1,3}[A-Z]$)|(^[0-9]{1,4}[A-Z]{1,2}$)|(^[0-9]{1,3}[A-Z]{1,3}$)|(^[A-Z]{1,2}[0-9]{1,4}$)|(^[A-Z]{1,3}[0-9]{1,3}$)

Source information

Forked from here but updated to work in JavaScript, and added an optional space

DVLA website

Rules

Current

It consists of two letters, followed by two numbers that help identify the age of the vehicle, and three letters at the end.

Prefix

It consists of one letter that helps identify the age of the vehicle, followed by one, two or three numbers, and three letters at the end.

Suffix

It consists of three letters, followed by one, two or three numbers, and a single letter at the end that helps identify the age of the vehicle.

Dateless

It can consist of up to four numbers followed by up to three letters, or vice versa. The maximum number of characters is six.

A dateless plate could be more formally described as four alternative rules:

  1. Long Number Prefix: It can consist of up to four numbers followed by up to two letters.
  2. Short Number Prefix: It can consist of up to three numbers followed by up to three letters.
  3. Long Number Suffix: It can consist of up to two letters followed by up to four numbers.
  4. Short Number Suffix: It can consist of up to three letters followed by up to three numbers.

Validation Examples

== Valid Current Style ==
AB51ABC
-- Invalid Current Style --
- Missing First Letter = Prefix e.g. A51ABC
- Extra First Letter -
ABC54ABC
- Missing Number -
AB5ABC
- Extra Number -
AB543ABC
- Missing Last Letter -
AB54AB
- Extra Last Letter -
AB54ABCD
== Valid Prefix ==
A123ABC
A12ABC
A1ABC
-- Invalid Prefix --
- Missing First Letter = Dateless e.g. 123ABC
- Extra First Letter -
AB1ABC
- Missing Numbers -
AABC
- Extra Numbers -
A1234ABC
- Missing Last Letter -
A1AB
- Extra Last Letter -
A1ABCD
== Valid Suffix ==
ABC123A
ABC12A
ABC1A
-- Invalid Suffix --
- Missing First Letter -
AB1A
AB123A
- Extra First Letter
ABCD1A
ABCD123A
- Missing Numbers -
ABCA
- Extra Numbers -
ABC1234A
- Missing Last Letter = Dateless e.g. ABC123
- Extra Last Letter -
ABC1AB
== Valid Dateless ==
1ABC
ABC1
1234A
A1234
1234AB
AB1234
123ABC
ABC123
-- Invalid Dateless --
- Missing Numbers -
ABC
- Extra Numbers -
12345A
A12345
- Missing Letters -
1
123
1234
- Extra Letters
1ABCD
ABCD1
- More than 6 characters -
1234ABC
ABC1234
@SavageCore
Copy link

FYI, Does not work with Northern Irish plates

Easy enough fix from what I understand. Just add |(^[A-Z]{3}[0-9]{1,4}$) to the end. DVLA website says that NI uses 3 letters followed by up to 4 numbers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment