Jelajahi Sumber

調整電話格式 E.164

SyuanYu 7 bulan lalu
induk
melakukan
ef9cedd022
1 mengubah file dengan 19 tambahan dan 1 penghapusan
  1. 19 1
      index.html

+ 19 - 1
index.html

@@ -2116,6 +2116,21 @@
       }
     }
 
+    // 調整為 E.164 電話格式
+    function formatPhoneNumberToE164(phoneNumber, countryCode) {
+      // 移除所有非數字字符
+      const cleaned = phoneNumber.replace(/\D/g, '');
+
+      // 移除電話號碼開頭的「0」
+      const withoutLeadingZero = cleaned.replace(/^0+/, '');
+
+      // 號碼以 + 開頭並加上國碼
+      if (!withoutLeadingZero.startsWith(countryCode.replace('+', ''))) {
+        return `${countryCode}${withoutLeadingZero}`;
+      }
+      return `+${withoutLeadingZero}`;
+    }
+
     // 驗證所有欄位
     function validateAllFields(fields) {
       let isValid = true;
@@ -2202,6 +2217,9 @@
         return;
       }
 
+      const countryCode = "+886"; // 台灣國家代碼
+      phone = formatPhoneNumberToE164(phone, countryCode);
+
       let data = {
         name: name, // 姓名
         phone: phone, // 手機
@@ -2339,7 +2357,7 @@
       $.ajax({
         method: "get",
         //url: "https://m3.hhh.com.tw:18670/gen_request_columns?filename=realtime.md",
-		url:"./json/index_columns.json",
+        url: "./json/index_columns.json",
         data: JSON.stringify(data),
         contentType: "application/json",
       }).done((response) => {