REGEXVAULTv2.0
Identity & PII/Passport Numbers
Verified Safe

US Passport Number Regex for Java

/^[A-Z][0-9]{8}$|^[A-Z]{2}[0-9]{7}$/

What this pattern does

This page provides a well-structured, multi-part regular expression for matching us passport number, ported and verified for Java. Identity and credential patterns need both correctness and safety, since they're frequent targets for adversarial input. The snippet below is ready to drop into your Java project — whether you're validating in a Spring Boot controller, a Jakarta EE service, or a standalone utility class.

Java Implementation

Java
// US Passport Number
// ReDoS-safe | RegexVault — Identity & PII > Passport Numbers

import java.util.regex.Pattern;

public class UsPassportNumberValidator {
    private static final Pattern PATTERN =
        Pattern.compile("^[A-Z][0-9]{8}$|^[A-Z]{2}[0-9]{7}$");

    public static boolean validate(String input) {
        return PATTERN.matcher(input).matches();
    }

    // Example
    public static void main(String[] args) {
        System.out.println(validate("A12345678")); // true
    }
}

Test Cases

Matches (Valid)
Rejects (Invalid)
A1234567812345678A
AB1234567A1234567
Z99999999A123456789
a12345678

When to use this pattern

This pattern is drawn from the Identity & PII > Passport Numbers category and carries a ReDoS-safe certification. That matters for Java developers because critical in Java applications since the JVM regex engine uses backtracking and is susceptible to ReDoS without careful pattern design. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.

Common Pitfalls

US passport numbers are sequential and do not encode personally identifying information (unlike some other countries). However, they are still highly sensitive as they are the primary document for international travel.

Technical Notes

US passport books use 1 letter + 8 digits. US passport cards also use 9 alphanumeric characters. Older US passports may have different formats. The State Department does not publish the exact checksum algorithm.

Have a pattern that belongs in the vault?

Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.

Submit a Pattern