New blogMaking an AI Agent You Can Trust With Money

★ India · regex

Regex for Aadhaar Number Format

Checks the shape of an Aadhaar number: 12 digits, first digit 2–9, optionally grouped 4-4-4 with spaces or hyphens. It does NOT validate the Verhoeff checksum digit, so a matching number can still be invalid.

  • Runs in your browser
  • No signup

Try the aadhaar number format pattern

Read it in plain English (best-effort)

  • ^start of a line
  • [2-9]one character from: 2–9
  • [0-9]one character from: 0–9
  • {3}exactly 3 of the previous
  • [ -]one character from: space, '-'
  • ?the previous is optional
  • [0-9]one character from: 0–9
  • {4}exactly 4 of the previous
  • [ -]one character from: space, '-'
  • ?the previous is optional
  • [0-9]one character from: 0–9
  • {4}exactly 4 of the previous
  • $end of a line

One sample per line works well with the m flag on anchored patterns.

Waiting for a pattern
01

The pattern, piece by piece

/^[2-9][0-9]{3}[ -]?[0-9]{4}[ -]?[0-9]{4}$/
Pattern explanation
PartWhat it does
^[2-9]the first digit is never 0 or 1
[0-9]{3}rest of the first group of four
[ -]?[0-9]{4}second group, optional separator
[ -]?[0-9]{4}$third group
02

What it accepts and rejects

Every sample below is checked automatically in JavaScript and Python. The tester above is preloaded with all of them, one per line.

Should match
Should match
234567890123✓
2345 6789 0123✓
2345-6789-0123✓
987654321098✓
5000 0000 0000✓
9999 9999 9999✓
Should not match
Should not match
123456789012✗
023456789012✗
23456789012✗
2345678901234✗
2345 67890 123✗
ABCD 6789 0123✗
03

Copy it into your code

RE2-safeNo lookarounds or backreferences, so the same pattern works in Go's RE2 engine and in Rust's regex crate.

const aadhaarFormatRe = /^[2-9][0-9]{3}[ -]?[0-9]{4}[ -]?[0-9]{4}$/;

export function isValidAadhaarFormat(value) {
  return aadhaarFormatRe.test(value.trim());
}
04

When not to use regex for this

Aadhaar numbers end in a Verhoeff check digit, which is arithmetic, not a pattern. More importantly, Aadhaar data is regulated: only collect it if you're legally allowed to, verify through UIDAI-authorised channels, and never store full numbers unmasked.

05

Questions engineers actually ask

Does a matching number mean the Aadhaar is valid?
No. This is format only; the Verhoeff checksum isn't checked, and only UIDAI authentication confirms a real Aadhaar.
Can I store Aadhaar numbers?
Only within what the law and UIDAI regulations allow for your use case. Masking all but the last four digits is standard practice.
What is catastrophic backtracking?
Some patterns, usually nested quantifiers like (a+)+, make a backtracking engine try an exponential number of paths on input that almost matches. The tester stops any run after 300ms, so a bad pattern can't freeze your tab. In production, that same pattern can take down a server (a ReDoS attack).
06

More india patterns

07

More from the workbench

Fine print

Tools: on the house.
Engineer: by the hour.

The regex was free. Code that doesn't need one is the upgrade: typed schemas, validated once at the edge, trusted everywhere after.

I'm Himanshu, an AI and full-stack engineer. I build MVPs, AI agents and the growth plumbing around them for early-stage startups.