SSN Regex Javascript Validator

Search...

⌘K

SSN Regex Javascript Validator

Search...

⌘K


SSN Regex Javascript Validator

Easily validate U.S. Social Security Numbers using our JavaScript Regex Tester. This tool ensures your input follows the standard XXX-XX-XXXX format. Whether you’re building secure onboarding flows or cleaning form data, pair it with our Base64 Encoder to protect sensitive info, or convert bulk files using the CSV to JSON Converter. Fast, accurate, and perfect for developers handling identity data.

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
Test your APIs today!

Write in plain English — Qodex turns it into secure, ready-to-run tests.

Regular Expression - Documentation

What is an SSN Regex?


In the U.S., a Social Security Number (SSN) is a 9-digit number formatted as XXX-XX-XXXX. Validating this format is crucial in applications where identity verification or data entry accuracy is required.


A regular expression (regex) helps ensure the format is correct before any sensitive operations are performed.


Common SSN Regex Pattern


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


What This Pattern Does:

  • ^\d{3}: Start with exactly 3 digits

  • -: A hyphen

  • \d{2}: Followed by 2 digits

  • -: Another hyphen

  • \d{4}$: Ends with 4 digits


Valid SSN: 123-45-6789

Invalid SSN: 12-3456-789 or 123456789


SSN Validation: Beyond the Basics


While matching the XXX-XX-XXXX format is a good start, a truly valid SSN must also meet a few more specific criteria:

  • 9 Digits Only: The SSN must have exactly 9 digits, no more, no less.

  • Hyphenated in 3 Parts: The number is always split into three parts by hyphens: the first part (3 digits), the second part (2 digits), and the third part (4 digits).

  • First Part Restrictions: The first three digits cannot be , , or any number in the range .

  • Second Part Range: The middle two digits must be between and — is not valid.

  • Third Part Range: The last four digits must be between and — is not valid.

For example, while is valid, numbers like , , or would fail these checks.

By combining regex formatting and these additional rules, you can more confidently validate SSNs and filter out invalid or potentially fraudulent entries.


Performance Considerations


When it comes to validating an SSN with this regex pattern, the process examines each character in the input string once, making the time complexity linear—O(N), where N is the number of characters. The space requirements are minimal, as the regex engine does not need extra memory proportional to the size of the input. In short, you get efficient validation both in speed and memory use.


How to Validate SSNs in JavaScript


Here’s a complete JavaScript code example:

function isValidSSN(ssn) {
  const ssnRegex = /^\d{3}-\d{2}-\d{4}$/;
  return ssnRegex.test(ssn);
}

// Example usage:
console.log(isValidSSN("123-45-6789")); // true
console.log(isValidSSN("123456789"));   // false


How to Validate SSNs in Python


Need to ensure SSNs fit the proper mold in your Python application? Let's walk through an easy approach using regular expressions—great for backend checks or data migrations.

Here's a typical function to validate SSN format in Python:

import re

def is_valid_ssn(ssn):
    pattern = r"^(?!6660009\d{2})\d{3}-(?!00)\d{2}-(?!0000)\d{4}$"
    return bool(re.match(pattern, ssn))

How it works:

  • ^(?!6660009\d{2})\d{3}: The SSN can't start with 666, 000, or any number between 900-999.

  • -(?!00)\d{2}: The middle two digits can't be 00.

  • -(?!0000)\d{4}$: The last four digits can't be 0000.

Examples:

print(is_valid_ssn("856-45-6789"))   # True – valid SSN
print(is_valid_ssn("000-45-6789"))  # False – invalid, leading block not allowed
print(is_valid_ssn("856-452-6789")) # False – wrong format
print(is_valid_ssn("856-45-0000"))  # False – trailing block not allowed

This approach quickly weeds out most SSN format issues and can easily be adapted for batch processing or user input validation. For extra protection, pair this with secure storage solutions—never keep SSNs in plain sight!


How to Validate SSNs Using Regex in Java


Just like in JavaScript, you can also use a regular expression in Java to check Social Security Number formats efficiently—and even add a bit more rigor.

Here’s what a typical validation function looks like in Java:

import java.util.regex.Pattern;

public class SSNValidator {
    // Pattern blocks obviously invalid ranges (000, 666, 900–999 in the first group; 00 in the middle; 0000 at the end)
    private static final Pattern SSN_REGEX = Pattern.compile(
        "^(?!6660009\\d{2})\\d{3}-(?!00)\\d{2}-(?!0{4})\\d{4}$"
    );

    public static boolean isValidSSN(String ssn) {
        if (ssn == null) return false;
        return SSN_REGEX.matcher(ssn).matches();
    }

    // Example usage
    public static void main(String[] args) {
        System.out.println(isValidSSN("856-45-6789")); // true
        System.out.println(isValidSSN("000-45-6789")); // false
        System.out.println(isValidSSN("856-452-6789")); // false
        System.out.println(isValidSSN("856-45-0000")); // false
    }
}

How it Works:

  • The regex prevents common invalid SSNs, such as those starting with 666, 000, or any 900–999 series, as well as entries with all zeros in the middle or end.

  • The function returns true only if the input matches both SSN format and these stricter rules.

  • As always, make sure to check for null before attempting the match to avoid exceptions.

This approach provides an extra layer of safety, catching not only mistyped formats but also values explicitly blocked by the Social Security Administration.


Where Can SSN Regex Be Used?


  • User Onboarding: Ensure users enter valid SSNs in financial or HR applications.

  • Database Integrity: Catch format errors before saving to your database.

  • Form Validation: Block submissions that don’t follow the expected SSN structure.


Use our JavaScript Regex Tester to experiment with variations or build custom patterns.


How to Validate SSNs in C#


If you’re working with .NET or C#, you can also use regular expressions to enforce correct SSN formatting and weed out common placeholder or invalid numbers. Here’s how you can implement a robust SSN validator in C#:

using System.Text.RegularExpressions;

public static bool IsValidSSN(string ssn)
{
    // This regex:
    // - Prevents area numbers 666, 000, or anything starting with 9
    // - Ensures group (middle) isn’t 00, and serial (last four) isn’t 0000
    var pattern = @"^(?!6660009\d{2})\d{3}-(?!00)\d{2}-(?!0{4})\d{4}$";
    return Regex.IsMatch(ssn, pattern

Example usage:

var testSSNs = new[] { "856-45-6789", "000-45-6789", "856-452-6789", "856-45-0000" };
foreach (var s in testSSNs)
{
    Console.WriteLine(IsValidSSN(s)); // Prints True or False

  • "856-45-6789" → Valid (True)

  • "000-45-6789" → Invalid (area cannot be 000)

  • "856-452-6789" → Invalid (bad format)

  • "856-45-0000" → Invalid (serial cannot be 0000)

This approach lets you safeguard your application from the most common SSN input mistakes and helps keep your identity data cleaner from the start.


SSN Validation in C++ Using Regex


If you’re working in C++ and need to ensure Social Security Numbers are correctly formatted, you can easily incorporate SSN regex validation right into your codebase.

Here’s a practical approach:

  1. Include the Required Libraries:
    Make sure you have the <regex> and <string> headers available.

  2. Write the Validation Function:
    Create a function that receives the SSN string and returns whether it matches the standard pattern.

#include 
#include 
#include 

// Checks if SSN matches the standard pattern
bool isValidSSN(const std::string& ssn) {
    // Prevents invalid area, group, and serial numbers
    std::regex ssnPattern(R"(^(?!6660009\d{2})\d{3}-(?!00)\d{2}-(?!0{4})\d{4}$)");
    return std::regex_match(ssn, ssnPattern);
}
  1. Sample Usage:

int main() {
    std::cout << std::boolalpha;
    std::cout << isValidSSN("856-45-6789") << std::endl; // true
    std::cout << isValidSSN("000-45-6789") << std::endl; // false
    std::cout << isValidSSN("856-452-6789") << std::endl; // false
    std::cout << isValidSSN("856-45-0000") << std::endl; // false
    return 0;
}

How it works:

  • The pattern rejects SSNs starting with 666, 000, or any 9XX series.

  • It blocks group numbers like “00” and serial numbers like “0000”—both invalid.

  • This setup lets you catch mistyped or clearly invalid SSNs before they reach your application’s sensitive workflows.

Try adapting the regex for your business rules, or extend it for batch validation in larger datasets.


Pro Tips


  • Use .test() for fast checks in live form validation.

  • Avoid storing SSNs as plain text. Use the Base64 Encoder to obfuscate values before transmission.

  • For added security, combine regex validation with server-side checks and encryption.

  • Regularly test your regex with mock data using our Random String Generator or Token Generator.

  • Use the CSV to JSON Converter if you’re batch-validating SSNs from user-uploaded files.


Combine with These Tools


Frequently asked questions

Does this regex ensure the SSN is real?×
No. It only checks the format, not whether the number is officially issued.
Can I use this for international IDs?+
Can SSNs have spaces instead of dashes?+
Will this work for real-time form validation?+
Is storing SSNs with regex validation safe?+
Can I import Figma designs?+
Is it SEO-friendly?+
Can I collaborate with my team?+
Is hosting included?+
Can I export code?+
Is there a free plan?+
Can I use custom fonts?+

SSN Regex Javascript Validator

Search...

⌘K

SSN Regex Javascript Validator

Search...

⌘K


SSN Regex Javascript Validator

SSN Regex Javascript Validator

Easily validate U.S. Social Security Numbers using our JavaScript Regex Tester. This tool ensures your input follows the standard XXX-XX-XXXX format. Whether you’re building secure onboarding flows or cleaning form data, pair it with our Base64 Encoder to protect sensitive info, or convert bulk files using the CSV to JSON Converter. Fast, accurate, and perfect for developers handling identity data.

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
Test your APIs today!

Write in plain English — Qodex turns it into secure, ready-to-run tests.

Regular Expression - Documentation

What is an SSN Regex?


In the U.S., a Social Security Number (SSN) is a 9-digit number formatted as XXX-XX-XXXX. Validating this format is crucial in applications where identity verification or data entry accuracy is required.


A regular expression (regex) helps ensure the format is correct before any sensitive operations are performed.


Common SSN Regex Pattern


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


What This Pattern Does:

  • ^\d{3}: Start with exactly 3 digits

  • -: A hyphen

  • \d{2}: Followed by 2 digits

  • -: Another hyphen

  • \d{4}$: Ends with 4 digits


Valid SSN: 123-45-6789

Invalid SSN: 12-3456-789 or 123456789


SSN Validation: Beyond the Basics


While matching the XXX-XX-XXXX format is a good start, a truly valid SSN must also meet a few more specific criteria:

  • 9 Digits Only: The SSN must have exactly 9 digits, no more, no less.

  • Hyphenated in 3 Parts: The number is always split into three parts by hyphens: the first part (3 digits), the second part (2 digits), and the third part (4 digits).

  • First Part Restrictions: The first three digits cannot be , , or any number in the range .

  • Second Part Range: The middle two digits must be between and — is not valid.

  • Third Part Range: The last four digits must be between and — is not valid.

For example, while is valid, numbers like , , or would fail these checks.

By combining regex formatting and these additional rules, you can more confidently validate SSNs and filter out invalid or potentially fraudulent entries.


Performance Considerations


When it comes to validating an SSN with this regex pattern, the process examines each character in the input string once, making the time complexity linear—O(N), where N is the number of characters. The space requirements are minimal, as the regex engine does not need extra memory proportional to the size of the input. In short, you get efficient validation both in speed and memory use.


How to Validate SSNs in JavaScript


Here’s a complete JavaScript code example:

function isValidSSN(ssn) {
  const ssnRegex = /^\d{3}-\d{2}-\d{4}$/;
  return ssnRegex.test(ssn);
}

// Example usage:
console.log(isValidSSN("123-45-6789")); // true
console.log(isValidSSN("123456789"));   // false


How to Validate SSNs in Python


Need to ensure SSNs fit the proper mold in your Python application? Let's walk through an easy approach using regular expressions—great for backend checks or data migrations.

Here's a typical function to validate SSN format in Python:

import re

def is_valid_ssn(ssn):
    pattern = r"^(?!6660009\d{2})\d{3}-(?!00)\d{2}-(?!0000)\d{4}$"
    return bool(re.match(pattern, ssn))

How it works:

  • ^(?!6660009\d{2})\d{3}: The SSN can't start with 666, 000, or any number between 900-999.

  • -(?!00)\d{2}: The middle two digits can't be 00.

  • -(?!0000)\d{4}$: The last four digits can't be 0000.

Examples:

print(is_valid_ssn("856-45-6789"))   # True – valid SSN
print(is_valid_ssn("000-45-6789"))  # False – invalid, leading block not allowed
print(is_valid_ssn("856-452-6789")) # False – wrong format
print(is_valid_ssn("856-45-0000"))  # False – trailing block not allowed

This approach quickly weeds out most SSN format issues and can easily be adapted for batch processing or user input validation. For extra protection, pair this with secure storage solutions—never keep SSNs in plain sight!


How to Validate SSNs Using Regex in Java


Just like in JavaScript, you can also use a regular expression in Java to check Social Security Number formats efficiently—and even add a bit more rigor.

Here’s what a typical validation function looks like in Java:

import java.util.regex.Pattern;

public class SSNValidator {
    // Pattern blocks obviously invalid ranges (000, 666, 900–999 in the first group; 00 in the middle; 0000 at the end)
    private static final Pattern SSN_REGEX = Pattern.compile(
        "^(?!6660009\\d{2})\\d{3}-(?!00)\\d{2}-(?!0{4})\\d{4}$"
    );

    public static boolean isValidSSN(String ssn) {
        if (ssn == null) return false;
        return SSN_REGEX.matcher(ssn).matches();
    }

    // Example usage
    public static void main(String[] args) {
        System.out.println(isValidSSN("856-45-6789")); // true
        System.out.println(isValidSSN("000-45-6789")); // false
        System.out.println(isValidSSN("856-452-6789")); // false
        System.out.println(isValidSSN("856-45-0000")); // false
    }
}

How it Works:

  • The regex prevents common invalid SSNs, such as those starting with 666, 000, or any 900–999 series, as well as entries with all zeros in the middle or end.

  • The function returns true only if the input matches both SSN format and these stricter rules.

  • As always, make sure to check for null before attempting the match to avoid exceptions.

This approach provides an extra layer of safety, catching not only mistyped formats but also values explicitly blocked by the Social Security Administration.


Where Can SSN Regex Be Used?


  • User Onboarding: Ensure users enter valid SSNs in financial or HR applications.

  • Database Integrity: Catch format errors before saving to your database.

  • Form Validation: Block submissions that don’t follow the expected SSN structure.


Use our JavaScript Regex Tester to experiment with variations or build custom patterns.


How to Validate SSNs in C#


If you’re working with .NET or C#, you can also use regular expressions to enforce correct SSN formatting and weed out common placeholder or invalid numbers. Here’s how you can implement a robust SSN validator in C#:

using System.Text.RegularExpressions;

public static bool IsValidSSN(string ssn)
{
    // This regex:
    // - Prevents area numbers 666, 000, or anything starting with 9
    // - Ensures group (middle) isn’t 00, and serial (last four) isn’t 0000
    var pattern = @"^(?!6660009\d{2})\d{3}-(?!00)\d{2}-(?!0{4})\d{4}$";
    return Regex.IsMatch(ssn, pattern

Example usage:

var testSSNs = new[] { "856-45-6789", "000-45-6789", "856-452-6789", "856-45-0000" };
foreach (var s in testSSNs)
{
    Console.WriteLine(IsValidSSN(s)); // Prints True or False

  • "856-45-6789" → Valid (True)

  • "000-45-6789" → Invalid (area cannot be 000)

  • "856-452-6789" → Invalid (bad format)

  • "856-45-0000" → Invalid (serial cannot be 0000)

This approach lets you safeguard your application from the most common SSN input mistakes and helps keep your identity data cleaner from the start.


SSN Validation in C++ Using Regex


If you’re working in C++ and need to ensure Social Security Numbers are correctly formatted, you can easily incorporate SSN regex validation right into your codebase.

Here’s a practical approach:

  1. Include the Required Libraries:
    Make sure you have the <regex> and <string> headers available.

  2. Write the Validation Function:
    Create a function that receives the SSN string and returns whether it matches the standard pattern.

#include 
#include 
#include 

// Checks if SSN matches the standard pattern
bool isValidSSN(const std::string& ssn) {
    // Prevents invalid area, group, and serial numbers
    std::regex ssnPattern(R"(^(?!6660009\d{2})\d{3}-(?!00)\d{2}-(?!0{4})\d{4}$)");
    return std::regex_match(ssn, ssnPattern);
}
  1. Sample Usage:

int main() {
    std::cout << std::boolalpha;
    std::cout << isValidSSN("856-45-6789") << std::endl; // true
    std::cout << isValidSSN("000-45-6789") << std::endl; // false
    std::cout << isValidSSN("856-452-6789") << std::endl; // false
    std::cout << isValidSSN("856-45-0000") << std::endl; // false
    return 0;
}

How it works:

  • The pattern rejects SSNs starting with 666, 000, or any 9XX series.

  • It blocks group numbers like “00” and serial numbers like “0000”—both invalid.

  • This setup lets you catch mistyped or clearly invalid SSNs before they reach your application’s sensitive workflows.

Try adapting the regex for your business rules, or extend it for batch validation in larger datasets.


Pro Tips


  • Use .test() for fast checks in live form validation.

  • Avoid storing SSNs as plain text. Use the Base64 Encoder to obfuscate values before transmission.

  • For added security, combine regex validation with server-side checks and encryption.

  • Regularly test your regex with mock data using our Random String Generator or Token Generator.

  • Use the CSV to JSON Converter if you’re batch-validating SSNs from user-uploaded files.


Combine with These Tools