Aller au contenu
uencoder

Write phone nombre validation regex

Phone nombres vary wildly par country. Apprendre practical regex motifs pour commun formats et pourquoi un phone library est souvent 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
    Testeur de regex →

    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
    Extracteur de téléphones du texte →

    Extraire 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.

Questions fréquentes

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.

Outils associés