跳到主要内容
uencoder

Write phone 数字 validation regex

Phone numbers 变化 wildly 通过 country. Learn practical regex patterns 为常见 formats 和为什么 phone 库是 经常 better.

  1. 1

    Phone 数字 formats 变化通过 country: 我们 (555-123-4567, (555) 123-4567, +1 555 123 4567), UK (+44 20 7946 0958), international (+CC...). practical 我们模式: ^\+?1?[-.\s]?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$ accepts 常见 formats. 为 international, use 库 like libphonenumber — regex cannot 处理所有 countries.

  2. 2
    正则表达式测试器 →

    Test phone patterns. 我们: ^\+?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}$ 是更多 permissive. Test 针对有效和 无效 numbers.

  3. 3
    文本电话号码提取器 →

    提取 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.

常见问题

应该 I use regex 或 phone 库?
为单个 country, regex 是 fine. 为 international numbers, use libphonenumber (Google) 或 similar — 它 handles country-specific formats, validates 面积 codes, 和 formats numbers 正确. Phone 数字 rules 是 too 复杂为 单个 regex.
应该 I 存储 formatted 或 normalized phone numbers?
Store normalized (E.164 格式化: +15551234567) 在数据库. Display formatted (+1 555-123-4567) 在 UI. Normalization removes 所有 formatting 和 adds country 代码, enabling consistent comparison 和 dialing. Use 库到 normalize 和格式化.

相关工具