SSN Regex Java Validator

Search...

⌘K

SSN Regex Java Validator

Search...

⌘K


SSN Regex Java Validator

SSN Regex Java Validator

The SSN Regex Java Validator is a powerful online utility for verifying U.S. Social Security Numbers (SSNs) in Java-based applications. It helps developers instantly test whether input strings match the official SSN format using Java-compatible regex patterns.


For complete form validation, consider combining this with tools like Email Regex Java Validator and Phone Number Regex Java Validator.

111-23-9023
Possible security issues
This regex appears to be safe.
Explanation
  • [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
Match information
Match 1: "111-23-9023" at index 0
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) is a 9-digit identifier issued by the U.S. government. The standard format is:


AAA-GG-SSSS


Where:

  • AAA is the area number

  • GG is the group number

  • SSSS is the serial number


Regex is commonly used to validate this structure in input forms, APIs, and databases.


Java SSN Regex Pattern

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


Explanation:

  • ^ and $ → Ensure the string matches the pattern from beginning to end

  • \\d{3} → Matches 3 digits (area number)

  • - → Matches the hyphen separator

  • \\d{2} → Matches 2 digits (group number)

  • \\d{4} → Matches 4 digits (serial number)


This pattern validates only properly formatted SSNs (e.g., 123-45-6789).


Java Code Example


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");
        }
    }
}


Examples

  • 123-45-6789 → Valid

  • 123456789 → Missing hyphens

  • 12-345-6789 → Invalid groupings

  • abc-de-ghij → Contains non-numeric characters


Pro Tips

  • Don’t store SSNs in plaintext. Always encrypt or hash when saving to databases.

  • If accepting SSNs from international users, ensure your regex restricts entries to valid U.S. formats.

  • Use input masks on front-end forms to guide users toward the correct format (###-##-####).

  • Avoid using placeholder SSNs like 123-45-6789 in production data—they’re publicly known and insecure.

  • The regex validates format only — not whether the SSN is officially issued.


Use Cases

  • HR Systems: Ensure employee SSNs are correctly formatted during onboarding.

  • Financial Services: Validate identity before processing loans or payments.

  • Government Forms: Automatically reject improperly formatted SSNs.

  • Databases: Clean up and standardize SSN fields.


Combine with These Tools

Frequently asked questions

Can this regex confirm if an SSN is real?×
No. It only checks if the input matches the correct format. It does not verify issuance or identity.
Are SSNs always in the same format?+
Can I remove the hyphens in the regex?+
Is this validator case-sensitive?+
Can I use this pattern in production?+