Write phone número validation regex
Phone números vary wildly por country. Aprender practical regex patrones para común formats y por qué un phone library es a menudo better.
- 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 Probador 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 Extractor de teléfonos del texto →
Extraer 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.
Preguntas frecuentes
- 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.