Identity
US Driver Licence (Generic) Regex
Every US state sets its own driver licence number format, ranging from all-digit numbers to a letter followed by many digits. This generic pattern covers the large share of formats that use zero, one or two leading letters followed by six to twelve digits, which is useful for car rental, insurance and age-gated sign-up forms that serve all states. It expects uppercase input with no separators. The honest limit is coverage: a few states use letters in the middle or more complex layouts, so for strict checking, apply a per-state pattern instead.
Pattern (JavaScript)
/^[A-Z]{0,2}[0-9]{6,12}$/Starts at the start of the line/string, then an uppercase letter, between 0 and 2 times, then a digit, between 6 and 12 times, then the end of the line/string.
Limit: Checks the shape only; it cannot tell whether a document is real or valid.
Examples
Matches
- ✓ A1234567
- ✓ 123456789
- ✓ WD12345678
Does not match
- ✗ AB12
- ✗ ABC123456
- ✗ A1234-567
Try it
0 matches (JavaScript engine)
A1234567
123456789
WD12345678
AB12
ABC123456
A1234-567Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^[A-Z]{0,2}[0-9]{6,12}$/g;
const text = "A1234567";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^[A-Z]{0,2}[0-9]{6,12}$')
text = 'A1234567'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^[A-Z]{0,2}[0-9]{6,12}$/';
$text = 'A1234567';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^[A-Z]{0,2}[0-9]{6,12}$");
String text = "A1234567";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^[A-Z]{0,2}[0-9]{6,12}$`)
text := "A1234567"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^[A-Z]{0,2}[0-9]{6,12}$");
var text = "A1234567";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^[A-Z]{0,2}[0-9]{6,12}$").unwrap();
let text = "A1234567";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^[A-Z]{0,2}[0-9]{6,12}$/
text = 'A1234567'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why not use one exact pattern per state?
- Per-state patterns are stricter and better when you also collect the issuing state. This generic version is useful when the state is unknown or when you just want to reject obvious garbage.
- Will this accept licences with dashes or spaces?
- No. Several states print separators, so strip spaces and dashes from the input before testing, then store the cleaned value.
- Can I tell the issuing state from the number alone?
- Not reliably, because many states share similar shapes such as plain eight or nine digit numbers. Ask for the state as a separate field.