萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 腳本程式專題 >> JavaScript

JavaScript 正則驗證手機號與電話號碼:支援 +86 國碼、多格式完整規則

需求分析

  • 手機:中國大陸 11 位,支援 `+86`、`86`、`+86-` 前綴,主流號段 13/14/15/16/17/18/19 開頭
  • 座機:區碼 3-4 碼 + 號碼 7-8 碼 + 分機 3-4 碼,支援 `+86` 前綴、分隔符 `-`/` `
  • 驗證回饋:即時提示、按鈕狀態控制

現代手機號正則(2024 主流號段)


/**
 * 中國大陸手機號驗證
 * 支援:13x, 14[014-9], 15[0-35-9], 16[2567], 17[0-8], 18x, 19[13589]
 * 選填 +86 / 86 / +86- / 86- 前綴
 */
const MOBILE_REGEX = /^(?:\+?86[- ]?)?1(?:3\d|4[0-9]|5[0-35-9]|6[2567]|7[0-8]|8\d|9[13589])\d{8}$/;

// 測試
["13800138000", "+86 13800138000", "86-13800138000", "1380013800"].forEach(n => 
  console.log(n, MOBILE_REGEX.test(n))
);
// true, true, true, false

座機電話正則


/**
 * 座機驗證
 * 格式:(+86|86)? 區碼(3-4碼) - 號碼(7-8碼) [- 分機(3-4碼)]?
 * 區碼首位不為 0(實際區碼以 0 開頭,但常省略 0),這裡允許 0 開頭
 */
const TEL_REGEX = /^(?:\+?86[- ]?)?(?:0\d{2,3}[- ]?)?\d{7,8}(?:[- ]?\d{3,4})?$/;

// 測試
["010-12345678", "021-87654321-123", "+86 10 12345678", "12345678"].forEach(n =>
  console.log(n, TEL_REGEX.test(n))
);
// true, true, true, true (純號碼也通過,視業務決定是否要求區碼)

完整驗證模組


const Validator = {
  mobile: MOBILE_REGEX,
  tel: TEL_REGEX,

  checkMobile(val) {
    return this.mobile.test(val.trim());
  },

  checkTel(val) {
    return this.tel.test(val.trim());
  },

  // 綁定輸入框即時驗證
  bind(inputSel, type, feedbackSel, btnSel) {
    const $input = document.querySelector(inputSel);
    const $feedback = document.querySelector(feedbackSel);
    const $btn = document.querySelector(btnSel);

    $input.addEventListener("input", () => {
      const val = $input.value.trim();
      const ok = type === "mobile" ? this.checkMobile(val) : this.checkTel(val);
      $feedback.textContent = ok ? "" : `請輸入正確的${type === "mobile" ? "手機" : "電話"}號碼`;
      $feedback.className = ok ? "valid" : "error";
      if ($btn) $btn.disabled = !ok;
    });
  }
};

// 使用
Validator.bind("#txtMobile", "mobile", "#mobileMsg", "#btnSubmit");
Validator.bind("#txtTel", "tel", "#telMsg", "#btnSubmit");

HTML 範例


<div>
  <label for="txtMobile">手機號碼(支援 +86):</label>
  <input type="tel" id="txtMobile" autocomplete="tel" placeholder="13800138000">
  <span id="mobileMsg" class="error"></span>
</div>
<div>
  <label for="txtTel">電話號碼(支援 +86):</label>
  <input type="tel" id="txtTel" autocomplete="tel" placeholder="010-12345678">
  <span id="telMsg" class="error"></span>
</div>
<button id="btnSubmit" disabled>提交</button>

進階:libphonenumber-js(Google 官方庫)

若需**全球號碼支援、格式化、國家碼解析**,強烈建議引入:


import { parsePhoneNumberFromString } from "libphonenumber-js";

const phone = parsePhoneNumberFromString("+86 138 0013 8000");
console.log(phone.isValid());      // true
console.log(phone.formatNational()); // "138 0013 8000"
console.log(phone.getType());      // "MOBILE"

常見坑點

坑點解決
正則未更新新號段(如 199、166)定期同步工信號段公告或用 libphonenumber-js
未 trim 空白導致驗證失敗驗證前 `.trim()`
型別為 number 導致前導 0 消失input type="tel"、一律當字串處理
只驗證前端、後端未驗證前後端**必須雙重驗證**

總結

手機/電話驗證核心在於**正則覆蓋最新號段**、**支援國碼前綴**、**前後端一致**。簡單專案用維護良好的正則;複雜/國際化專案直接上 `libphonenumber-js`,一勞永逸。

copyright © 萬盛學電腦網 all rights reserved