美国电话号码格式:10位数字、E.164 与开发校验
美国电话号码格式
美国电话号码使用 NANP (North American Numbering Plan) 体系,所有号码均为 10 位数字。
号码构成
| 部分 | 长度 | 说明 |
|---|---|---|
| 国家代码 | 1 | `+1` |
| 区号 (Area Code) | 3 | 3 位数字 |
| 中缀 (Exchange) | 3 | 3 位数字 (NXX) |
| 线路号 (Line Number) | 4 | 4 位数字 |
完整号码:
```
+1 415 555 1234
1 415 555 1234
415 555 1234
(415) 555-1234
415-555-1234
4155551234
```
区号规则
```javascript
// 区号正则
const areaCodeRegex = /^[2-9]\d{2}$/;
// 完整号码正则
const phoneRegex = /^\(?([2-9]\d{2})\)?[\s.-]?([2-9]\d{2})[\s.-]?(\d{4})$/;
```
主要城市区号
| 城市 | 主要区号 |
|---|---|
| New York | 212, 718, 917, 646 |
| Los Angeles | 213, 310, 323, 424 |
| Chicago | 312, 773, 872 |
| Houston | 281, 713, 832 |
| Phoenix | 480, 602, 623 |
| San Francisco | 415, 628 |
| Seattle | 206, 425 |
| Miami | 305, 786 |
| Boston | 617, 857 |
| Las Vegas | 702, 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 位 |
总结
美国电话号码设计的最佳实践:
掌握这些原则能让你的电话字段设计更专业、更易用。