SSN Regex Go Validator

Search...

⌘K

SSN Regex Go Validator

Search...

⌘K


SSN Regex Go Validator

SSN Regex Go Validator

Need to verify U.S. Social Security Numbers during form validation or API testing? The Qodex SSN Regex Go Validator helps you check SSNs against standard formats instantly. Pair it with tools like the Username Generator, Phone Number Generator, or Address Generator to simulate realistic user data for testing your Go applications.

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?


In Go (or Golang), regular expressions are handled via the built-in regexp package, allowing developers to match and validate text with precision.


A Social Security Number (SSN) in the U.S. has a fixed format: AAA-GG-SSSS, where:


  • AAA is the area number (3 digits)

  • GG is the group number (2 digits)

  • SSSS is the serial number (4 digits)


To validate this structure, regex provides a simple and efficient solution using the pattern:


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


This pattern ensures:

  • Three digits

  • Followed by a dash

  • Then two digits

  • Another dash

  • And four final digits


Example Inputs

To help you visualize what this regex catches and what it skips, here are some sample SSN-like inputs and their match results:

  • ✔️ Valid SSN format

  • ✔️ Valid, but with spaces instead of dashes

  • ✔️ Valid SSN format

  • ❌ Invalid—contains letters

  • ✔️ Valid, but may fall into restricted SSN ranges

  • ✔️ Valid pattern, but not a real SSN

  • ❌ Invalid—area number cannot be 000

This variety ensures your tests can distinguish between properly formatted SSNs, common input mistakes, and outright invalid entries. Use these examples to fine-tune your validation or to seed realistic test data during development.


Understanding Regex Flags


When working with regular expressions in Go or JavaScript, you’ll often come across flags that modify how your patterns behave. Here’s a quick reference to the most common ones:

  • g (global): Searches for all matches in the text, not just the first one.

  • i (ignore case): Makes the match case-insensitive, treating uppercase and lowercase letters as equal.

  • m (multiline): Changes the behavior of ^ and $ so they match at the start and end of each line, rather than just the start and end of the string.

  • s (dotAll): Allows the dot (.) to match newline characters, so your pattern can span multiple lines.

  • u (unicode): Enables full Unicode matching, making it easier to handle emoji and characters from non-English scripts.

  • y (sticky): Matches only at the last index where the previous match ended, helpful for iterating through a string one match at a time.

Each flag tailors your regex to suit specific validation or searching needs, so it’s worth experimenting to see which combination fits your use case best.


How Invalid SSNs Are Blocked


Not all combinations are valid for U.S. Social Security Numbers—and the regex pattern is smart enough to catch common invalid formats. Here’s how it guards against bogus entries:

  • No All-Zero Groups: The regex won’t accept any SSN where a full group is zeroed out, like , , or . This protects against placeholders and unrealistic numbers.

  • No 666 Starts: Any SSN beginning with is always rejected—this sequence is never assigned to real SSNs.

  • No 900–999 Ranges: The pattern also rules out SSNs starting with through , which are reserved and not valid for standard identification.

Thanks to these checks, you minimize the risk of processing obviously fake or reserved SSNs, helping your apps and API endpoints remain secure and reliable.


Core Features & Benefits


  • Format-Specific: Strictly enforces U.S. SSN format.

  • Instant Validation: Works immediately in Go with regexp.MustCompile.

  • Safe Testing: Avoid using real data—test your systems with dummy values.

  • Reusable Regex: Easily incorporate this pattern into your Go codebase.

  • Compatible with Multiple Tools: Works great with UUID Generator, MAC Address Generator, and Phone Number Regex Go Validator for full simulation.



Example Code in Go


package main

import (
    "fmt"
    "regexp"
)

func isValidSSN(ssn string) bool {
    // Regex for U.S. SSN: 3 digits - 2 digits - 4 digits
    var ssnRegex = regexp.MustCompile(`^\d{3}-\d{2}-\d{4}$`)
    return ssnRegex.MatchString(ssn)
}

func main() {
    testSSN := "123-45-6789"
    fmt.Printf("Is '%s' a valid SSN? %t\n", testSSN, isValidSSN(testSSN))
}


Pro Tips for Using SSN Regex in Go


  • Use Anchors for Full Match: Always wrap your regex with ^ and $ to ensure the entire string is validated and not just a substring.

  • Avoid Real Data in Tests: Never use actual SSNs in testing environments. Instead, simulate them using tools like the Username Generator, Phone Number Generator, or Address Generator to create complete mock profiles.

  • Precompile for Performance: Use regexp.MustCompile() outside of loops or functions to precompile your regex and improve performance, especially in large-scale apps.

  • Validate Format, Not Identity: Regex only checks structure. To validate real SSNs, integrate with a verification API or official database.

  • Use Logging for Debugging: While testing your Go regex logic, add logs to track matches and mismatches. This will help catch formatting issues early.


Common Use Cases


  • Form Validation: Ensure users enter SSNs in the correct format.

  • Onboarding Systems: Verify identification during user registration.

  • Data Cleansing: Clean up and standardize SSN fields in your database.

  • Compliance Systems: Enforce format validation for privacy-sensitive workflows.


You’ll also find these regex basics useful:


Expression What it matches Any digit (equivalent to ) The word "hello" repeated 1 to 3 times consecutively The beginning of a line or string The end of a line or string These elements are essential when crafting patterns to match Social Security Numbers or any structured data in your Go applications.


Combine With These Tools


Use the SSN Validator with these Qodex tools for complete profile simulation and testing:



Regex for Other Languages


Want to test SSNs in other languages?


Frequently asked questions

Can this regex check if an SSN is real?×
No, it only checks if the SSN is correctly formatted—not whether it’s issued or valid.
Why use regex for SSN validation in Go?+
What if someone enters an SSN without dashes?+
Is it safe to store SSNs after validating them?+
Can this be used in production systems?+