Identity
India Passport Number Regex
Indian passport numbers consist of a single uppercase letter followed by seven digits. Commonly published format rules exclude the letters Q, X and Z in the first position and avoid zero as the first or last digit, and this pattern encodes those rules. It suits visa applications, airline bookings and HR onboarding forms that handle Indian travellers. Input should be uppercase and free of spaces before testing. An honest limit is that the letter rules come from observed issuing practice rather than a published regex specification, so a rare legitimate number could fall outside them.
Pattern (JavaScript)
/^[A-PR-WY][1-9][0-9]{5}[1-9]$/Starts at the start of the line/string, then one of the characters A-PR-WY, then one of the characters 1-9, then a digit, exactly 5 times, then one of the characters 1-9, 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
- ✓ M2345678
- ✓ K7654321
Does not match
- ✗ Q1234567
- ✗ A0234567
- ✗ A123456
Try it
0 matches (JavaScript engine)
A1234567
M2345678
K7654321
Q1234567
A0234567
A123456Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^[A-PR-WY][1-9][0-9]{5}[1-9]$/g;
const text = "A1234567";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^[A-PR-WY][1-9][0-9]{5}[1-9]$')
text = 'A1234567'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^[A-PR-WY][1-9][0-9]{5}[1-9]$/';
$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-PR-WY][1-9][0-9]{5}[1-9]$");
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-PR-WY][1-9][0-9]{5}[1-9]$`)
text := "A1234567"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^[A-PR-WY][1-9][0-9]{5}[1-9]$");
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-PR-WY][1-9][0-9]{5}[1-9]$").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-PR-WY][1-9][0-9]{5}[1-9]$/
text = 'A1234567'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why are Q, X and Z excluded from the first letter?
- Those letters are widely reported as unused for the first character of ordinary Indian passport numbers. Excluding them catches some typing errors, but you can widen the class to [A-Z] if you prefer to be permissive.
- Why must the first and last digits be non-zero?
- Common format guidance for Indian passports avoids zero in those positions, so the pattern uses [1-9] there. This also rejects many accidental entries that start with a padded zero.
- Can I use this pattern on text that contains other words?
- Not as written, because the anchors require the whole input to be the number. To search inside free text, replace the anchors with word boundaries, for example \b at each end.