コンテンツへスキップ
メニュー
この質問にフラグが付けられました
2 返信
2328 ビュー

hello 
I'm trying to add the area code to phone numbers. I used an inherited method where the phone numbers should be formatted, but when I add a number to the phone field, my code doesn't work as expected.

I have an issue where, when I add a number to the field and press enter or click elsewhere, the format according to my code should be +52 1 xx xxxx xxxx. However, for some numbers, it doesn't respect this format and appears as +52 xx xxxx xxxx, and in some cases as +52 xxx xxx xxxx. 

Here is my code:


def format_phone(self, phone_number): 
​clean_number = re.sub(r'\D', '', phone_number) # Remove all non-numeric characters if if ​len(clean_number) == 10: # If it's a 10-digit number 

​return '+52 1 ' + clean_number[:2] + ' ' + ​clean_number[2:6] + ' ' + clean_number[6:] ​else: return phone_number # Return the number unchanged ​if it doesn't have 10 digits

 

@api.onchange('phone') def _onchange_phone(self): 

​if self.phone: 

​phone_pattern = re.compile(r'^\+52\s1\s\d{2}\s\d{4}\s\d{4}$') # Expected format 

​clean_number = re.sub(r'\D', '', self.phone) 

​if not phone_pattern.match(self.phone) and len(clean_number) == 10: 

​self.phone = ​self.format_phone(self.phone) 

​​ ​_logger.info("Formatted phone number: %s", self.phone)


@api.onchange('mobile') 

def _onchange_mobile(self): 

​if self.mobile: 

​phone_pattern = re.compile(r'^\+52\s1\s\d{2}\s\d{4}\s\d{4}$') # Expected format 

​clean_number = re.sub(r'\D', '', self.mobile) 

​​if not phone_pattern.match(self.mobile) and len(clean_number) == 10:

​  ​self.mobile = self.format_phone(self.mobile) 

​_logger.info("Formatted mobile number: %s", self.mobile)
 

アバター
破棄
最善の回答

Hello Ing. Ang3l Barrera,


Hope you are doing well.


I have shared the code below. Please check my note and use it accordingly.


Note:


If you enter your mobile or phone number and the default country code is included in your field,

please add it in the following line to replace the starting country code ('+49' is used as an example country code):

clean_number = self.phone.replace("+49 ", "").replace(" ", "")


//Code in Comment//


Thanks & Regards,

Email:  odoo@aktivsoftware.com           

Skype: kalpeshmaheshwari

アバター
破棄

Code :

@api.onchange('mobile')
def onchange_mobile(self):
if self.mobile:
number = self._format_mobile_numbers(self.mobile)
if number:
self.mobile = number

@api.onchange('phone')
def onchange_phone(self):
if self.phone:
number = self._format_mobile_numbers(self.phone)
if number:
self.phone = number

def _format_mobile_numbers(self, number):
clean_number = self.phone.replace("+49 ", "").replace(" ", "")
original_numer = clean_number[-10:]
if len(original_numer) == 10:
return self.format_phone_number(original_numer)

def format_phone_number(self, number):
number = re.sub(r'\D', '', number)
if len(number) == 10:
formatted_number = f"+52 1 {number[:2]} {number[2:6]} {number[6:]}"
return formatted_number

著作者 最善の回答

Thanks, Jainesh Shah.

I tried using your code, but I encountered a problem.

After some research, I found a solution in the validation_format module, where I used...

 If country == "MX":

​return f'number'

now my code is okay.
thanks

アバター
破棄
関連投稿 返信 ビュー 活動
4
6月 25
2178
1
6月 25
1106
0
2月 25
970
1
11月 24
1940
1
11月 24
1115