Identity
Generic Passport Number Regex
Most passport numbers around the world are short runs of uppercase letters and digits, usually between six and nine characters long. This pattern accepts exactly that envelope, which makes it a sensible first filter for travel booking forms, visa intake screens or hotel check-in tools that serve guests from many countries. It rejects spaces, dashes and lowercase input, so normalise user input before testing. The honest limit is breadth: because it is deliberately generic, it will also accept strings such as postcodes or product codes that merely share the same length and alphabet.
Pattern (JavaScript)
/^[A-Z0-9]{6,9}$/Starts at the start of the line/string, then one of the characters A-Z0-9, between 6 and 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
- ✓ AB1234567
- ✓ X9876543
- ✓ 123456789
Does not match
- ✗ AB12
- ✗ ab1234567
- ✗ AB-123456
Try it
0 matches (JavaScript engine)
AB1234567
X9876543
123456789
AB12
ab1234567
AB-123456Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^[A-Z0-9]{6,9}$/g;
const text = "AB1234567";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^[A-Z0-9]{6,9}$')
text = 'AB1234567'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^[A-Z0-9]{6,9}$/';
$text = 'AB1234567';
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-Z0-9]{6,9}$");
String text = "AB1234567";
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-Z0-9]{6,9}$`)
text := "AB1234567"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^[A-Z0-9]{6,9}$");
var text = "AB1234567";
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-Z0-9]{6,9}$").unwrap();
let text = "AB1234567";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^[A-Z0-9]{6,9}$/
text = 'AB1234567'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why does this pattern reject lowercase letters?
- Passport numbers are printed in uppercase, so lowercase input usually means the user typed it casually. Convert the input with toUpperCase() before testing, or add the i flag if you prefer to accept either case.
- Should I use this or a country-specific passport pattern?
- Use a country-specific pattern when you know the issuing country, because it rejects far more mistakes. Use this generic one when the country is unknown or when your form accepts passports from dozens of countries.
- Does a match mean the passport number exists?
- No. The pattern only checks length and allowed characters. Confirming that a passport was actually issued requires the issuing authority or an approved document verification service.