返回经验列表
电话号码NANP开发参考

美国电话号码格式:10位数字、E.164 与开发校验

Petmind2026-06-225分钟阅读

美国电话号码格式

美国电话号码使用 NANP (North American Numbering Plan) 体系,所有号码均为 10 位数字。

号码构成

部分长度说明
国家代码1`+1`
区号 (Area Code)33 位数字
中缀 (Exchange)33 位数字 (NXX)
线路号 (Line Number)44 位数字

完整号码:

```

+1 415 555 1234

1 415 555 1234

415 555 1234

(415) 555-1234

415-555-1234

4155551234

```

区号规则

  • 3 位数字
  • N 位:2-9(不能 0 或 1)
  • X 位:0-9
  • X 位:0-9
  • ```javascript

    // 区号正则

    const areaCodeRegex = /^[2-9]\d{2}$/;

    // 完整号码正则

    const phoneRegex = /^\(?([2-9]\d{2})\)?[\s.-]?([2-9]\d{2})[\s.-]?(\d{4})$/;

    ```

    主要城市区号

    城市主要区号
    New York212, 718, 917, 646
    Los Angeles213, 310, 323, 424
    Chicago312, 773, 872
    Houston281, 713, 832
    Phoenix480, 602, 623
    San Francisco415, 628
    Seattle206, 425
    Miami305, 786
    Boston617, 857
    Las Vegas702, 725

    常见格式

    人类可读

    ```

    (415) 555-1234

    415-555-1234

    415.555.1234

    ```

    E.164 国际标准

    ```

    +14155551234

    ```

    数据库存储

    ```

    14155551234 (10位)

    ```

    测试用号码

    号码用途
    555-01XX美国测试 (常见)
    (555) 123-4567影视作品
    +1 555 0100通用测试

    校验函数

    ```javascript

    function validateUSPhone(phone) {

    // 去除所有非数字字符

    const digits = phone.replace(/\D/g, '');

    // 美国号码应为 10 位或 11 位 (含国家代码)

    if (digits.length === 10) {

    return isValidNANP(digits);

    }

    if (digits.length === 11 && digits[0] === '1') {

    return isValidNANP(digits.substring(1));

    }

    return false;

    }

    function isValidNANP(digits) {

    // 区号检查:第一位 2-9

    if (!/^[2-9]/.test(digits.substring(0, 3))) {

    return false;

    }

    // 中缀检查:第一位 2-9

    if (!/^[2-9]/.test(digits.substring(3, 6))) {

    return false;

    }

    return digits.length === 10;

    }

    ```

    格式化函数

    ```javascript

    // 输入:14155551234

    // 输出:(415) 555-1234

    function formatPhone(digits) {

    const cleaned = digits.replace(/\D/g, '');

    if (cleaned.length === 10) {

    return `(${cleaned.substring(0, 3)}) ${cleaned.substring(3, 6)}-${cleaned.substring(6)}`;

    }

    if (cleaned.length === 11 && cleaned[0] === '1') {

    return `+1 (${cleaned.substring(1, 4)}) ${cleaned.substring(4, 7)}-${cleaned.substring(7)}`;

    }

    return digits;

    }

    ```

    表单设计

    基础输入框

    ```html

    <label>Phone Number *</label>

    <input

    type="tel"

    name="phone"

    placeholder="(415) 555-1234"

    pattern="\(\d{3}\) \d{3}-\d{4}"

    inputmode="tel"

    >

    ```

    多字段拆分

    ```html

    <label>Phone Number *</label>

    <div class="phone-fields">

    <input name="area_code" placeholder="415" maxlength="3">

    <input name="exchange" placeholder="555" maxlength="3">

    <input name="line" placeholder="1234" maxlength="4">

    </div>

    ```

    数据库设计

    ```sql

    CREATE TABLE contacts (

    id BIGINT PRIMARY KEY,

    phone_country_code CHAR(1) DEFAULT '1',

    phone_area_code CHAR(3) NOT NULL,

    phone_exchange CHAR(3) NOT NULL,

    phone_line CHAR(4) NOT NULL,

    -- 派生字段

    phone_e164 VARCHAR(15) GENERATED ALWAYS AS (

    CONCAT('+', phone_country_code, phone_area_code, phone_exchange, phone_line)

    ) STORED,

    INDEX idx_phone (phone_e164)

    );

    ```

    在地址表单中的应用

    在地址表单中,电话号码通常作为必填字段或选填字段:

    场景必填校验
    收货地址严格 10 位
    联系信息严格 10 位
    紧急联系人宽松校验
    账单地址仅 10 位

    总结

    美国电话号码设计的最佳实践:

  • 统一存储格式:E.164 国际标准
  • 人类可读展示:(xxx) xxx-xxxx
  • 测试数据规范:使用 555-01XX 测试号段
  • 校验逻辑严格:区号首位不能为 0/1
  • inputmode 优化:移动端用 `inputmode="tel"`
  • 掌握这些原则能让你的电话字段设计更专业、更易用。

    返回经验列表