Identity
UK Passport Number Regex
British passports issued in recent decades carry a nine-digit document number printed on the photo page and in the machine readable zone. This pattern accepts exactly nine digits with no letters, spaces or separators, making it a quick check for UK-focused travel, employment right-to-work or visa forms. Because the rule is so simple, it is easy to port to any language without changes. The limit is that any nine-digit string passes, including phone fragments or account numbers, so pair it with a clear field label and keep the document type explicit.
Pattern (JavaScript)
/^[0-9]{9}$/Starts at the start of the line/string, then a digit, exactly 9 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
- ✓ 123456789
- ✓ 987654321
- ✓ 501234567
Does not match
- ✗ 12345678
- ✗ 1234567890
- ✗ 12345678A
Try it
0 matches (JavaScript engine)
123456789
987654321
501234567
12345678
1234567890
12345678ACode in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^[0-9]{9}$/g;
const text = "123456789";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^[0-9]{9}$')
text = '123456789'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^[0-9]{9}$/';
$text = '123456789';
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("^[0-9]{9}$");
String text = "123456789";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^[0-9]{9}$`)
text := "123456789"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^[0-9]{9}$");
var text = "123456789";
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"^[0-9]{9}$").unwrap();
let text = "123456789";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^[0-9]{9}$/
text = '123456789'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Do UK passport numbers ever contain letters?
- Standard British passport books use nine digits. Some older or special documents used different layouts, so if you must accept those, loosen the pattern or provide a separate free-text option.
- Should I allow spaces between digit groups?
- The number is printed without spaces, but users sometimes add them when copying. Remove whitespace before testing rather than complicating the pattern with optional separators.
- Why is the pattern anchored at both ends?
- Without the caret and dollar anchors, a search would find nine digits inside a longer string such as a ten-digit number. Anchoring forces the whole input to be exactly nine digits.