Skip to Content
Menu
You need to be registered to interact with the community.
This question has been flagged
2 Odgovori
2338 Prikazi

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)
 

Avatar
Opusti
Best Answer

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

Avatar
Opusti

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

Avtor Best Answer

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

Avatar
Opusti
Related Posts Odgovori Prikazi Aktivnost
4
jun. 25
2181
1
jun. 25
1117
0
feb. 25
971
1
nov. 24
1942
1
nov. 24
1115