Browse Source

fleat: update customer phone check

igb 2 ngày trước cách đây
mục cha
commit
296c4ed4fc
2 tập tin đã thay đổi với 110 bổ sung4 xóa
  1. 60 2
      customerAdd.php
  2. 50 2
      customerEdit.php

+ 60 - 2
customerAdd.php

@@ -29,6 +29,15 @@ checkLogin();
             upMediaExt:"wmv,avi,wma,mp3,mid"
         });
         
+        // 失去焦点时执行格式化
+        $(document).on('blur', '.method-input', function() {
+            var methodType = $(this).prev('.method-select').val();
+            if (methodType === 'tel' || methodType === 'whatsapp') {
+                var formattedValue = formatPhoneNumber($(this).val(), true);
+                $(this).val(formattedValue);
+            }
+        });
+        
         // Add contact form
         $('.add-contact-btn').click(function() {
             var contactsContainer = $('#contacts-container');
@@ -86,6 +95,16 @@ checkLogin();
             
             methodsContainer.append(methodRow);
             updateMethodFields(methodsContainer.find('.contact-method-row:last-child'));
+            
+            // 初始化新添加的方法行的输入事件
+            var newRow = methodsContainer.find('.contact-method-row:last-child');
+            newRow.find('.method-select').on('change', function() {
+                var methodType = $(this).val();
+                if (methodType === 'tel' || methodType === 'whatsapp') {
+                    // 清除已有内容并设置正确的placeholder
+                    $(this).next('.method-input').val('');
+                }
+            });
         });
         
         // Remove contact method
@@ -116,6 +135,36 @@ checkLogin();
         }
     });
 
+    // 格式化电话号码函数
+    function formatPhoneNumber(phone, finalFormat = false) {
+        if (!phone) return phone;
+        
+        // 去除所有非数字、加号和空格
+        phone = phone.replace(/[^\d\s+]/g, '');
+        
+        // 去除开头和结尾的空格
+        phone = phone.trim();
+        
+        // 确保以加号开头
+        if (!phone.startsWith('+')) {
+            // 如果第一个字符是数字,添加加号
+            if (/^\d/.test(phone)) {
+                phone = '+' + phone;
+            } else {
+                // 如果不是以数字开头,检查是否有数字并添加加号
+                var firstDigitIndex = phone.search(/\d/);
+                if (firstDigitIndex >= 0) {
+                    phone = '+' + phone.substring(firstDigitIndex);
+                } else {
+                    // 如果没有数字,返回原始输入
+                    return phone;
+                }
+            }
+        }
+        
+        return phone;
+    }
+
     // Update method fields based on selection
     function updateMethodFields(methodRow) {
         var select = methodRow.find('select.method-select');
@@ -263,7 +312,7 @@ checkLogin();
         var hasContactMethod = false;
         var hasAlibabaContact = false;
         var allContactsValid = true;
-        var phoneRegex = /^\+\d{1,4}\s\d{5,}$/; // Regex to validate phone format: +[country code] [number]
+        var phoneRegex = /^\+\d{1,4}\s\d+$/; // 区号后必须有空格,号码中不能有空格
         var emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/; // Regex to validate email format
         
         $('.contact-form').each(function(contactIndex) {
@@ -296,7 +345,16 @@ checkLogin();
                 // Validate phone number format for tel and whatsapp
                 if ((methodType === 'tel' || methodType === 'whatsapp') && methodValue) {
                     if (!phoneRegex.test(methodValue)) {
-                        alert("电话格式不正确,请使用以下格式:区号+号码,如 +86 15012345678");
+                        // 检查具体的错误原因
+                        if (!methodValue.startsWith('+')) {
+                            alert("电话号码必须以'+'开头");
+                        } else if (methodValue.indexOf(' ') === -1) {
+                            alert("区号后必须有空格,例如: +86 15012345678");
+                        } else if (methodValue.split(' ').length > 2 || methodValue.split(' ')[1].indexOf(' ') !== -1) {
+                            alert("号码部分不能包含空格");
+                        } else {
+                            alert("电话格式不正确,正确格式为: +区号 号码,例如 +86 15012345678");
+                        }
                         $(this).find('input.method-input').focus();
                         allContactsValid = false;
                         return false;

+ 50 - 2
customerEdit.php

@@ -95,6 +95,15 @@ if (!empty($id) && is_numeric($id)) {
             hoverExecDelay:-1
         });
         
+        // 失去焦点时执行格式化
+        $(document).on('blur', '.method-input', function() {
+            var methodType = $(this).prev('.method-select').val();
+            if (methodType === 'tel' || methodType === 'whatsapp') {
+                var formattedValue = formatPhoneNumber($(this).val(), true);
+                $(this).val(formattedValue);
+            }
+        });
+        
         // Remove contact
         $(document).on('click', '.remove-contact-btn', function() {
             var contactForm = $(this).closest('.contact-form');
@@ -557,6 +566,36 @@ if (!empty($id) && is_numeric($id)) {
         $(selectElement).next('.method-input').attr('placeholder', placeholder);
     }
 
+    // 格式化电话号码函数
+    function formatPhoneNumber(phone, finalFormat = false) {
+        if (!phone) return phone;
+        
+        // 去除所有非数字、加号和空格
+        phone = phone.replace(/[^\d\s+]/g, '');
+        
+        // 去除开头和结尾的空格
+        phone = phone.trim();
+        
+        // 确保以加号开头
+        if (!phone.startsWith('+')) {
+            // 如果第一个字符是数字,添加加号
+            if (/^\d/.test(phone)) {
+                phone = '+' + phone;
+            } else {
+                // 如果不是以数字开头,检查是否有数字并添加加号
+                var firstDigitIndex = phone.search(/\d/);
+                if (firstDigitIndex >= 0) {
+                    phone = '+' + phone.substring(firstDigitIndex);
+                } else {
+                    // 如果没有数字,返回原始输入
+                    return phone;
+                }
+            }
+        }
+        
+        return phone;
+    }
+
     // Custom validation function for multiple contacts form with contact methods
     function validateMultipleContactsForm() {
         var clientCode = $("#cs_code").val();
@@ -600,7 +639,7 @@ if (!empty($id) && is_numeric($id)) {
         var hasContactMethod = false;
         var hasAlibabaContact = false;
         var allContactsValid = true;
-        var phoneRegex = /^\+\d{1,4}\s\d{5,}$/; // Regex to validate phone format: +[country code] [number]
+        var phoneRegex = /^\+\d{1,4}\s\d+$/; // 区号后必须有空格,号码中不能有空格
         var emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/; // Regex to validate email format
         
         $('.contact-form').each(function(contactIndex) {
@@ -633,7 +672,16 @@ if (!empty($id) && is_numeric($id)) {
                 // Validate phone number format for tel and whatsapp
                 if ((methodType === 'tel' || methodType === 'whatsapp') && methodValue) {
                     if (!phoneRegex.test(methodValue)) {
-                        alert("电话格式不正确,请使用以下格式:区号+号码,如 +86 15012345678");
+                        // 检查具体的错误原因
+                        if (!methodValue.startsWith('+')) {
+                            alert("电话号码必须以'+'开头");
+                        } else if (methodValue.indexOf(' ') === -1) {
+                            alert("区号后必须有空格,例如: +86 15012345678");
+                        } else if (methodValue.split(' ').length > 2 || methodValue.split(' ')[1].indexOf(' ') !== -1) {
+                            alert("号码部分不能包含空格");
+                        } else {
+                            alert("电话格式不正确,正确格式为: +区号 号码,例如 +86 15012345678");
+                        }
                         $(this).find('input.method-input').focus();
                         allContactsValid = false;
                         return false;