Skip to content

Regex for Ssn — Pattern, Examples & Caveats

Pattern
^\d{3}-\d{2}-\d{4}$

This pattern matches the standard US Social Security Number format: three digits, a hyphen, two digits, a hyphen, and four digits (AAA-GG-SSSS). It validates the textual shape only. Security-oriented patterns check sensitive or policy-bound input: password composition, card number shapes, tokens, private IP ranges and national identifiers. They are a first line of defense, never the whole story — pair them with checksums, real verification, encryption and access control. A regex can confirm a format but cannot prove a secret is valid or safe. It needs no special flags. Apply it to the whole input as a format contract. It accepts invalid SSNs like 000-00-0000 or area 666/900-999; real validation excludes those ranges and requires the hyphens or a 9-digit form. SSNs are highly sensitive PII — never log them, mask them in UI, and handle under strict privacy controls. It requires hyphens; the unhyphenated 9-digit form needs an alternative pattern. As with any regular expression, confirm its behavior in your target engine, test boundary and adversarial inputs, and watch for catastrophic backtracking before relying on it in production.

Advertisement
Ad slot · ref-top

Token-by-token explanation

  • ^ — start anchor
  • \d{3} — area number (three digits)
  • - — literal hyphen
  • \d{2} — group number (two digits)
  • - — literal hyphen
  • \d{4} — serial number (four digits)
  • $ — end anchor

What it matches

123-45-6789000-00-0000987-65-4321

What it does not match

123456789123-45-67812-345-6789ABC-DE-FGHI

When to use it

Reach for a security pattern as one layer of input handling for sensitive or policy-bound data: password rules, card shapes, tokens, private ranges and national identifiers. Never treat a match as proof of validity or safety — combine it with checksums, signature verification, breach screening, encryption and strict access control. Also guard against ReDoS by avoiding nested quantifiers and bounding input length, since security checks are a tempting target for denial-of-service abuse.

Caveats

  • It accepts invalid SSNs like 000-00-0000 or area 666/900-999; real validation excludes those ranges and requires the hyphens or a 9-digit form.
  • SSNs are highly sensitive PII — never log them, mask them in UI, and handle under strict privacy controls.
  • It requires hyphens; the unhyphenated 9-digit form needs an alternative pattern.

Code examples

JavaScript
const re = /^\d{3}-\d{2}-\d{4}$/;
const input = "123-45-6789";
if (re.test(input)) {
  // valid: proceed
} else {
  // reject with a helpful message
}
Python
import re

rx = re.compile(r"^\d{3}-\d{2}-\d{4}$")
value = "123-45-6789"
if rx.fullmatch(value):
    pass  # valid
else:
    raise ValueError("invalid format")
Java
import java.util.regex.*;

Pattern p = Pattern.compile("^\\d{3}-\\d{2}-\\d{4}$");
Matcher m = p.matcher("123-45-6789");
boolean ok = m.matches();   // whole input must conform
PHP
<?php
$subject = "123-45-6789";
if (preg_match('/^\d{3}-\d{2}-\d{4}$/', $subject, $m)) {
    // $m[0] is the full match
}

How to test it

Before trusting any pattern, test it against three kinds of input: valid examples that must match, invalid examples that must be rejected, and edge cases at the boundaries (empty strings, maximum lengths, unusual separators and Unicode). Paste the expression into a live regex tester with your actual sample data and confirm the highlighted matches are exactly what you intend. Watch for catastrophic backtracking by trying long non-matching inputs, and remember that engine differences (JavaScript, Python, Java, PCRE, Go/RE2) mean a pattern that works in one may fail or behave differently in another — especially lookarounds, which Go's RE2 does not support at all.

Frequently asked questions

Are all format-matching SSNs valid?

No — the SSA excludes certain area numbers (000, 666, 900-999), all-zero groups and serials. Format regex is only a first pass; apply the real rules or a validation service.

How should I handle SSNs securely?

Treat them as sensitive PII: encrypt at rest, mask in logs and UI (e.g. ***-**-6789), limit access, and never store them unless legally necessary.

Is a regex enough to secure sensitive input?

Never on its own. Regex checks format; security also needs checksums (Luhn), real verification (token signatures, breach screening), encryption, masking and access control. Treat the pattern as one layer, not the solution.

How do I prevent ReDoS in security checks?

Avoid nested unbounded quantifiers, set a match timeout where the engine supports it, cap input length, and prefer simple linear patterns. Test suspicious inputs with long non-matching strings before deploying.

Advertisement
Ad slot · ref-in-article

Try the Regex Tester

Live regex matching with groups, flags and explanations.

Open tool →