Formatting Phone Numbers
Status: Closed · Asked by Evergreen Admin on · 0 views
currently, our subscribers type in their US phone numbers in any format such as:
111 111 1111
111-111-1111
(111) 111 1111
1 111 111 1111
+1 111 111 1111
etc... is there a way to set it in Pabbly flow so that it converts all variations to a +1 111 111 1111 format, no matter in which way they enter?
thanks
Hello @em-admin , try this
Contact me - https://www.facebook.com/himeshsoni07
Email - [EMAIL][email hidden][/EMAIL]
thanks! in your screenshot I can see that Number Required is set to (111) 111 1111, does it mean that only when this format is entered, only then the formatting by Pabbly take place? what if a user enters 111-111-1111... will your suggested setup still work? and if wouldn't work, what is a solution to address all possible formats, as per my original question?
Yes it'll work for all number format just try.
Contact me - https://www.facebook.com/himeshsoni07
Email - [EMAIL][email hidden][/EMAIL]
thanks, I tried and it does not return as per example, with spaces or dashes in between. how can we achieve a number to be returned in a format +1 111 111 1111 or +1 111-111-1111 ?
Attachments

Please try using the following Python code, which should help you achieve your desired output.
def format_phone_number(phone_number):
# Remove any non-numeric characters from the phone number
clean_number = ''.join(filter(str.isdigit, phone_number))# Check if the number is valid and has at least 10 digits
if len(clean_number) >= 10:
# If the number already has the US country code, use it; otherwise, add it
if len(clean_number) >= 11 and clean_number[:2] == '1':
formatted_number = f"+{clean_number[:2]} {clean_number[2:5]} {clean_number[5:8]} {clean_number[8:]}"
else:
formatted_number = f"+1 {clean_number[-10:-7]} {clean_number[-7:-4]} {clean_number[-4:]}"
return formatted_number
else:
return "Invalid phone number"
# Example usage:
phone_number_input = "+1 987 654 3210"
formatted_number = format_phone_number(phone_number_input)
print(f"Formatted number: {formatted_number}")

this is great, thank you, appreciated!!