|
@@ -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;
|