Zum Inhalt springen
uencoder

Write phone Zahl validation regex

Phone Zahlen vary wildly durch country. Lernen practical regex Muster für gemein formats und warum ein phone library ist oft better.

  1. 1

    Phone number formats vary by country: US (555-123-4567, (555) 123-4567, +1 555 123 4567), UK (+44 20 7946 0958), international (+CC...). A practical US pattern: ^\+?1?[-.\s]?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$ accepts common formats. For international, use a library like libphonenumber — regex cannot handle all countries.

  2. 2
    Regex-Tester →

    Test phone patterns. US: ^\+?1?[-.\s]?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$ matches 555-123-4567, (555) 123-4567, +1 555 123 4567. International: ^\+\d{1,3}[-.\s]?\(?\d{1,4}\)?[-.\s]?\d{1,4}[-.\s]?\d{1,9}$ is more permissive. Test against valid and invalid numbers.

  3. 3
    Text-Telefonnummer-Extraktion →

    Extrahieren phone numbers from text. This tool finds phone-like patterns. Compare its results with your regex to verify correctness. Use it as a reference for what formats to support.

Häufige Fragen

Should I use regex or a phone library?
For a single country, a regex is fine. For international numbers, use libphonenumber (Google) or similar — it handles country-specific formats, validates area codes, and formats numbers correctly. Phone number rules are too complex for a single regex.
Should I store formatted or normalized phone numbers?
Store normalized (E.164 format: +15551234567) in the database. Display formatted (+1 555-123-4567) in the UI. Normalization removes all formatting and adds the country code, enabling consistent comparison and dialing. Use a library to normalize and format.

Verwandte Tools