Phone Regex Pattern API: Validate Phone Numbers in Any Format

Need to validate phone numbers in your application? This guide covers the Phone Regex Pattern API, which provides patterns for validating phone numbers in various formats including international, US, UK, and other country-specific formats.

Phone Number Formats

Phone numbers come in many formats. The same number can be written as:

  • 555-123-4567 (US dash format)
  • (555) 123-4567 (US with parentheses)
  • +1 555 123 4567 (International with spaces)
  • 5551234567 (Digits only)
  • +1-555-123-4567 (International with dashes)

Pattern Types

The API provides patterns for different validation needs:

  • US: North American phone numbers (10 digits)
  • UK: United Kingdom phone numbers
  • International: E.164 format with country code
  • Flexible: Accepts various common formats
  • Digits Only: Just the numeric digits
Recommendation: For the best user experience, use a "flexible" pattern that accepts multiple formats, then normalize the number for storage.

Using the Phone Regex API

TinyFn provides pre-built phone validation patterns:

API Request
GET https://api.tinyfn.io/v1/regex/patterns/phone
Headers: X-API-Key: your-api-key
Response
{
  "pattern": "^(\\+1[-\\s]?)?\\(?[0-9]{3}\\)?[-\\s]?[0-9]{3}[-\\s]?[0-9]{4}$",
  "type": "us",
  "description": "US phone number pattern (10 digits with optional +1)",
  "flags": "",
  "examples": {
    "valid": ["555-123-4567", "(555) 123-4567", "+1 555 123 4567"],
    "invalid": ["12345", "555-1234", "abc-def-ghij"]
  }
}

Parameters

Parameter Type Description
type string Pattern type: us, uk, international, flexible. Default: flexible
country string Country code for country-specific pattern (e.g., "DE", "FR")
require_country_code boolean Require country code prefix. Default: false

Code Examples

JavaScript / Node.js

// Get a flexible phone pattern
const response = await fetch(
  'https://api.tinyfn.io/v1/regex/patterns/phone?type=flexible',
  { headers: { 'X-API-Key': 'your-api-key' } }
);
const { pattern, flags } = await response.json();

// Validate various formats
const phoneRegex = new RegExp(pattern, flags);
console.log(phoneRegex.test('555-123-4567')); // true
console.log(phoneRegex.test('(555) 123-4567')); // true
console.log(phoneRegex.test('+1 555 123 4567')); // true

Python

import requests
import re

response = requests.get(
    'https://api.tinyfn.io/v1/regex/patterns/phone',
    params={'type': 'international'},
    headers={'X-API-Key': 'your-api-key'}
)
result = response.json()

phone_pattern = re.compile(result['pattern'])
phones = ['+44 20 7123 4567', '+1 212 555 1234', 'invalid']
for phone in phones:
    print(f"{phone}: {bool(phone_pattern.match(phone))}")

cURL

curl "https://api.tinyfn.io/v1/regex/patterns/phone?type=uk" \
  -H "X-API-Key: your-api-key"

Common Use Cases

  • Form Validation: Validate phone number inputs in registration forms
  • Contact Import: Validate phone numbers during data import
  • CRM Systems: Ensure phone number quality in customer records
  • SMS Services: Validate numbers before sending SMS
  • Two-Factor Auth: Validate phone numbers for 2FA setup

Best Practices

  1. Accept multiple formats: Don't force users into one format
  2. Normalize for storage: Store in E.164 format (+15551234567)
  3. Display formatted: Format for display based on locale
  4. Consider using libraries: For complex validation, consider libphonenumber

Use via MCP

Your AI agent can call this tool directly via Model Context Protocol — no HTTP code needed. Add TinyFn to Claude Desktop, Cursor, or any MCP client:

{
  "mcpServers": {
    "tinyfn-regex": {
      "url": "https://api.tinyfn.io/mcp/regex/",
      "headers": {
        "X-API-Key": "your-api-key"
      }
    }
  }
}

See all regex tools available via MCP in our Regex MCP Tools for AI Agents guide.

Try the Phone Regex Pattern API

Get your free API key and start validating phone numbers.

Get Free API Key

Ready to try TinyFn?

Get your free API key and start building in minutes.

Get Free API Key