
Use the SSN Regex Java Validator to validate U.S. Social Security Numbers in Java applications. Instantly check input strings against standard SSN format using Java regex. Ideal for secure forms, identity checks, and backend validations.
For complete form validation, consider combining this with tools like Email Regex Java Validator and Phone Number Regex Java Validator.
A Social Security Number (SSN) is a 9-digit identifier issued by the U.S. government. It follows this format:
AAA-GG-SSSS
Where:
AAA = Area number
GG = Group number
SSSS = Serial number
Regex is commonly used to validate this structure in Java applications, APIs, and form inputs.
"^\\d{3}-\\d{2}-\\d{4}$"Pattern Breakdown:
^ and $ → Anchors to ensure full-string match
\\d{3} → Matches the area (3 digits)
- → Hyphen separator
\\d{2} → Group (2 digits)
\\d{4} → Serial (4 digits)
This pattern validates properly formatted SSNs like 123-45-6789.
While the regex checks format, real SSNs must follow these rules:
No zero groups: 000-12-3456, 123-00-4567, or 123-45-0000 are invalid.
No prefix 666
No numbers starting with 900–999
These filters help eliminate fake or improperly issued SSNs.
import java.util.regex.Pattern; import java.util.regex.Matcher;public class SSNValidator { public static void main(String[] args) { String ssn = "123-45-6789"; String regex = "^\d{3}-\d{2}-\d{4}$";
Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(ssn); if (matcher.matches()) { System.out.println("Valid SSN"); } else { System.out.println("Invalid SSN format"); } }
}
✅ 123-45-6789 – Valid
❌ 123456789 – Missing hyphens
❌ 12-345-6789 – Wrong grouping
❌ abc-de-ghij – Contains letters
Use flags to modify how regex works:
Pattern.CASE_INSENSITIVE – Match regardless of case
Pattern.MULTILINE – Make ^ and $ work on each line
Pattern.DOTALL – Allow . to match newlines
Pattern.UNICODE_CASE – Handle Unicode characters
Use matcher.find() loop for multiple matches
Never store SSNs in plaintext — always hash or encrypt
Use input masks like ###-##-#### in forms
Avoid placeholder SSNs like 123-45-6789 in live systems
Regex checks format only — not whether the SSN is real or issued
Pair frontend masking with backend regex for best results
HR Systems – Validate employee details during onboarding
Finance Apps – Prevent invalid IDs for secure processing
Govt Portals – Enforce correct formats in public data forms
Data Cleanup – Detect and fix malformed SSNs in databases
Email Regex Java Validator – Validate user emails in the same form.
Phone Number Regex Java Validator – Validate U.S. phone numbers alongside SSNs.
Java Regex Tester – Experiment with custom SSN patterns.
Write in plain English — Qodex turns it into secure, ready-to-run tests.