Identity
US Passport Number Regex
United States passport books have used nine-digit numbers for decades, while newer Next Generation books start with a single letter followed by eight digits. This pattern accepts both shapes and nothing else, which keeps it tight enough for airline profile forms, travel insurance quotes or onboarding flows that only collect US documents. It does not accept spaces or separators, so strip them first. One honest limit: passport cards and older special-issue documents can follow different conventions, so if your users submit those, you may need a looser rule or a separate field.
Pattern (JavaScript)
/^(?:[0-9]{9}|[A-Z][0-9]{8})$/Starts at the start of the line/string, then (a digit, exactly 9 times OR an uppercase letter, then a digit, exactly 8 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
- ✓ A12345678
- ✓ C98765432
Does not match
- ✗ 12345678
- ✗ AB1234567
- ✗ A123456789
Try it
0 matches (JavaScript engine)
123456789
A12345678
C98765432
12345678
AB1234567
A123456789Code 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}|[A-Z][0-9]{8})$/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}|[A-Z][0-9]{8})$')
text = '123456789'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^(?:[0-9]{9}|[A-Z][0-9]{8})$/';
$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}|[A-Z][0-9]{8})$");
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}|[A-Z][0-9]{8})$`)
text := "123456789"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^(?:[0-9]{9}|[A-Z][0-9]{8})$");
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}|[A-Z][0-9]{8})$").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}|[A-Z][0-9]{8})$/
text = '123456789'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why are there two alternatives in the pattern?
- Older US passport books carry nine digits, while Next Generation books issued in recent years begin with a letter and then eight digits. The alternation accepts either form while still requiring exactly nine characters in total.
- Will this accept a US passport card number?
- Passport cards commonly start with the letter C followed by eight digits, which this pattern happens to accept. If you need to tell a card apart from a book, store the document type in a separate field rather than inferring it from the number.
- Can I use this pattern to stop fraud?
- It only stops obvious typing mistakes. A fabricated number with the right shape still matches, so fraud checks need an issuer lookup or a trusted identity verification provider.