Phone Number Regex Python Validator

Search...

⌘K

Phone Number Regex Python Validator

Search...

⌘K


Phone Number Regex Python Validator

Phone Number Regex Python Validator

Validate phone numbers accurately using the Phone Number Regex Python Validator. Whether you’re checking local formats or international patterns, this tool ensures precise validation tailored for Python applications. For extended testing, use the Python Regex Tester or dive deeper with utilities like the Python IP Address Regex Validator and Python Email Regex Validator.

(123) 456-7890
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: "(123) 456-7890" 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 Phone Number Regex in Python?


Phone number regex in Python allows you to validate if a string matches expected phone number formats—whether local, international, or formatted for readability.


In Python, this is typically done using the re module with a regular expression (regex) pattern that checks for digits, optional country codes, separators (like -,  , or ()), and valid lengths.


When to Use It?


  • User signup forms to ensure phone inputs are correct

  • APIs that process contact data

  • CRM systems to clean and validate mobile entries

  • SMS/Call systems to avoid message failure due to incorrect formats


Common Regex Patterns for Phone Numbers


  1. Basic digits only (10-digit US-style number):

    ^\d{10}$

    Matches 9876543210


  2. With country code (e.g., +91 for India):

    ^\+\d{1,3}\d{7,14}$

    Matches +919876543210


  3. Formatted with spaces or hyphens:

    ^\+?\d{1,3}[-\s]?\(?\d{3}\)?[-\s]?\d{3}[-\s]?\d{4}$

    Matches +1 800-555-1234 or (800) 555-1234


How to Validate Phone Numbers Using Regex in Python

Here’s a complete example using the re module:


import re

def is_valid_phone(phone):
    # Allows optional country code, spaces, dashes, parentheses
    pattern = re.compile(r'^\+?\d{1,3}?[-\s]?\(?\d{2,4}\)?[-\s]?\d{3,4}[-\s]?\d{4}$')
    return pattern.match(phone) is not None

# Test cases
print(is_valid_phone("+91 98765 43210"))      # True
print(is_valid_phone("(022) 123-4567"))       # True
print(is_valid_phone("9876543210"))           # True
print(is_valid_phone("12345"))                # False


Combine with These Tools



Pro Tips


  • Always normalize phone numbers after validation for storage (e.g., remove hyphens or spaces).

  • If dealing with international users, prefer patterns that support optional + and varying lengths.

  • Avoid assuming a fixed length; different countries have different standards (e.g., UK, India, US).

  • Use re.fullmatch() instead of re.match() if you want to strictly match the entire string.

  • For UI, consider pairing regex validation with dropdowns for country codes to improve accuracy.


Use Cases


  • Validating user input in Django or Flask forms

  • Filtering bulk contact data in ETL pipelines

  • Verifying phone numbers before sending SMS alerts

  • Adding regex constraints in Python-based data validators


Frequently asked questions

Does this regex support landline formats?×
Yes, the pattern can be customized to handle local landline formats including area codes in parentheses.
Can I use this regex for international formats?+
How do I extract only valid phone numbers from a text file?+
Is regex enough for validating real phone numbers?+
Should I store formatted or unformatted numbers?+