🔥 Master Regex Validation in Minutes!

Regular expressions (regex) are powerful tools for validating and manipulating text. Whether you're working with forms, data processing, or input validation, mastering regex can save you hours of development time. Here's a quick guide to some of the most useful regex patterns you'll need.
August 9, 2025

📧 Email Address Validation

Pattern:
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$

What it does:
Ensures the email consists of:

  • A valid local part (before the "@")
  • An "@" symbol
  • A domain with a proper top-level domain (at least 2 characters)

🌐 URL Validation

Pattern:
^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$

What it does:
Checks if the input is a valid HTTP or HTTPS URL, including:

  • Optional protocol (http:// or https://)
  • Valid domain name structure
  • Optional paths or query parameters

🔐 Strong Password Validation

Pattern:
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$

What it does:
Enforces a strong password with:

  • At least 8 characters
  • 1 lowercase letter
  • 1 uppercase letter
  • 1 digit
  • 1 special character (e.g., @, $, !, %, *, ?, &)

📅 Date Format (YYYY-MM-DD)

Pattern:
^(19|20)\d\d-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$

What it does:
Validates dates in ISO format, ensuring:

  • Year between 1900-2099
  • Month between 01-12
  • Day between 01-31 (with proper month/day validation)

🎨 Hexadecimal Color Code

Pattern:
^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$

What it does:
Matches 3-digit or 6-digit hex color codes, with an optional # prefix.

📞 US Phone Number Validation

Pattern:
^(\+1\s?)?(\([2-9][0-8][0-9]\)|[2-9][0-8][0-9])[-.\s]?([2-9][0-9]{2})[-.\s]?([0-9]{4})$

What it does:
Validates US phone numbers with:

  • Optional country code (+1)
  • Valid area code (e.g., [2-9][0-8][0-9])
  • Proper exchange code and line number
  • Supports separators like -, ., or spaces

🌍 IPv4 Address Validation

Pattern:
\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b

What it does:
Matches a valid IPv4 address, ensuring each octet is between 0 and 255.

🆔 US Social Security Number (SSN) Validation

Pattern:
^(?!000|666)[0-8]\d{2}-(?!00)\d{2}-(?!0000)\d{4}$

What it does:
Validates US SSNs while excluding:

  • Invalid sequences like 000, 666 in the first segment
  • 00 in the middle segment
  • 0000 in the last segment

🚀 Save These Regex Patterns for Later!

Bookmark this guide to quickly reference these essential regex validations in your projects. With these patterns, you can ensure data integrity and improve user input validation effortlessly.

Happy coding! 💻✨