LogoReturn to Home RegEx
Integrations
All integrations
AWS API
AWS Lambda
DynamoDB
Oracle
Redshift
Snowflake
GraphQL
Supabase
Twilio
Azure Blob Storage
Slack
SendGrid
Generic HTTP API
AWS S3
Stripe
Microsoft SQL
Salesforce
PostgreSQL
MySQL
MongoDB
HubSpot
Google Sheets
Google BigQuery
Firebase
Airtable
Integrations
About UI Bakery
Log in
Request UI Bakery demo
RegEx library
Email regex PHP
Phone number regex PHP
IP address regex PHP
Date regex PHP
URL regex PHP
Numbers only regex (digits only) PHP
UUID regex PHP
Regex match words PHP
ZIP code regex PHP
GUID regex PHP
Password regex PHP
HTML regex PHP
SSN regex PHP
XML regex PHP
Mac address regex PHP
Street address regex PHP

SSN regex PHP

SSN stands for social security number and is issued to US citizens, permanent and temporary residents.

Discover UI Bakery – an intuitive visual internal tools builder. Try it now!
JavaScript
Python
Java
C#
PHP
No items found.

SSN regex

This number has the following rules:

  • consists of 9 digits and usually divided by 3 parts by hyphen (XXX-XX-XXXX).
  • The first part can not be 000, 666, or between 900-900.
  • Second part can not be 00
  • Third part can not be 0000

Below is a simple SSN regular expression that is divided by hyphens.

  • If you want to make hyphens optional, put ? after each dash
  • If you want to validate SSN without hyphens - just remove them
"/^(?!666|000|9\\d{2})\\d{3}-(?!00)\\d{2}-(?!0{4})\\d{4}$/"
Test it!
/^(?!666|000|9\d{2})\d{3}-(?!00)\d{2}-(?!0{4})\d{4}$/

True

False

Enter a text in the input above to see the result

Example code in PHP:

// Validate SSN
$ssn_validation_regex = '/^(?!666|000|9\\d{2})\\d{3}-(?!00)\\d{2}-(?!0{4})\\d{4}$/'; 
echo preg_match($ssn_validation_regex, '100-22-3333'); // returns 1

// Extract SSN from a string
$extract_ssn_pattern = '/(?!666|000|9\\d{2})\\d{3}-(?!00)\\d{2}-(?!0{4})\\d{4}/'; 
$string_to_match = 'My SSN is 100-22-3333';
preg_match_all($extract_ssn_pattern, $string_to_match, $matches);
print_r($matches[0]) // matches[0] is ['100-22-3333']

‍

Test it!

True

False

Enter a text in the input above to see the result

Test it!

True

False

Enter a text in the input above to see the result

Create an internal tool with UI Bakery

Discover UI Bakery – an intuitive visual internal tools builder.

Try it now