ERPNext.com Frappe Cloud Support Partners Foundation Frappe School

Phone number formatting

Is it possible to format phone numbers in contact similar to how numbers are formatted from system settings?

I.E. we could enter 123456789 as contact’s phone, and it would format to (123) 456-789. A few options could be selectable from system settings, in the same section as date and number format.

I’m happy to help as our team has been asking for this - but I’m not sure how the Frappe team would want this enhancement to be done.

Regards,
Alec

@alec_ruizramon1 send a pull-request. Happy to look into it.

here is a suggetion.

  1. Add a phone format in System Settings
  2. In Data if value of options is “Phone” then format it using that format.

So I have done the above by being able to set options to Phone and then strip characters and re-format.

For US phone numbers it’s rather easy - either 10 or 11 digits, and really only a few ways to format them. When involving the international formats, it gets quite complicated…

I looked into https://github.com/daviddrysdale/python-phonenumbers (python port of Google’s libphonenumber library) but there isn’t a lot in terms of formatting - either spaces or dashes. See here:

 """
    Phone number format.
    INTERNATIONAL and NATIONAL formats are consistent with the definition in
    ITU-T Recommendation E123. For example, the number of the Google
    Switzerland office will be written as "+41 44 668 1800" in INTERNATIONAL
    format, and as "044 668 1800" in NATIONAL format.  E164 format is as per
    INTERNATIONAL format but with no formatting applied, e.g. "+41446681800".
    RFC3966 is as per INTERNATIONAL format, but with all spaces and other
    separating symbols replaced with a hyphen, and with any phone number
    extension appended with ";ext=". It also will have a prefix of "tel:"
    added, e.g. "tel:+41-44-668-1800".
    Note: If you are considering storing the number in a neutral format, you
    are highly advised to use the PhoneNumber class.
    """

So far, this is my best lead. I guess it would work well as a first effort if you agree?

Also, I have set the event to occur on insert & save, after the db update & before post-save-methods. I didn’t know a way to read from the field before it was saved in the db - if there is a way to do that, it may be better.

Let me know your thoughts & I’ll send a PR once I’ve made changes & tested.

Maybe it can be added here: https://github.com/frappe/frappe/blob/develop/frappe/model/document.py#L574

You will have to check if there is any field with Phone formatting and then fix this.

Or better, just make a general purpose wrapper method and let the developer call this the validate method of the controller to fix / validate phone numbers/

@rmehta here is what I have : https://github.com/aruizramon/frappe-1/commit/4099ce91619c0c2b9484af8efc8e7b90a5c5778c

You can see what I mean by the formatting being hard-coded.

My knowledge of Python is still limited, so if there is a better method I’m all ears!

this could be placed in /desk#Form/Country/Germany DocType where there are already times and dates: