Validate US Social Security Numbers (SSNs) effortlessly with the SSN Regex Python Validator. This tool ensures the correct “AAA-GG-SSSS” structure using Python’s re module. Also explore Email Regex Python Validator, Phone Number Regex Python Validator, and Python Regex Tester for broader input validation.
[A-Z]
: uppercase letters[a-z]
: lowercase letters[0-9]
: digits\.
: a literal dot+
: one or more of the preceding*
: zero or more of the preceding?
: optional (zero or one)^
: start of string$
: end of string
Test your APIs today!
Write in plain English — Qodex turns it into secure, ready-to-run tests.
Regular Expression - Documentation
What is SSN Regex?
A Social Security Number (SSN) in the U.S. follows the standard format:
XXX-XX-XXXX, where:
XXX: Area number (3 digits)
XX: Group number (2 digits)
XXXX: Serial number (4 digits)
This format ensures structured identification for tax and legal purposes. Using regex helps validate whether an input string follows this format strictly.
SSN Regex Pattern
The typical regex pattern to validate a properly formatted SSN is:
Breakdown:
^\d{3}
→ Three digits at the start-
→ First hyphen separator\d{2}
→ Two digits-
→ Second hyphen\d{4}$
→ Four digits at the end
This ensures strict format matching like 123-45-6789.
Python Example Code
Test regex variations with the Python Regex Tester.
Use Cases
User Identity Verification: Ensure accurate SSN input during user registration or onboarding.
Data Cleaning: Detect and correct improperly formatted SSNs in databases.
Security & Compliance: Verify SSN format before transmitting sensitive data.
You can combine this with:
Email Regex Python Validator for account verification
IP Address Regex Python Validator for system security
URL Regex Python Validator to secure form input fields
Regex Metacharacters Used
^
: Anchors the start of the string\d
: Matches any digit (0–9){n}
: Matches exactly n digits-
: Matches literal hyphens in SSNs$
: Anchors the end of the string
Pro Tips
Always anchor patterns with ^ and $ to avoid partial matches.
Preprocess user input to trim whitespaces before validation.
You can extend validation to exclude invalid combinations (like 000 or 666) using advanced regex or logic.
Avoid storing plain SSNs—encrypt them after validation.
Use Python Regex Tester to experiment before deploying.
Combine with Date Regex Python Validator if SSNs are part of a larger structured document.