DEV Community

Cover image for Phone Number Normalisation
Ajdin Mustafić for Bornfight

Posted on

Phone Number Normalisation

The phone number normalisation is used to translate a phone number into a standard form.

For example, there is E.164 international standard which defines format as "[+] [country code] [subscriber number including area code] and can have a maximum of fifteen digits". However, we rarely save numbers, especially domestic ones, in this exact format.

This can be a huge problem if you wanted to allow your users to import their mobile phone contacts into your application. The users can have the phone number, in their phone contacts, entered with or without country code, or can enter country codes like 00385 or +385, etc. So if you didn't have the phone number normalisation, these two numbers 00385991234567 and +385991234567 would be imported as two different phone numbers for the same contact even though when calling, they are the same, just written in different format standards.

We have a similar challenge on one of our projects, so our first step was to research, what should be covered and what others cover when they do the phone number normalisation.

One of my outputs were the test cases that are covered by Whatsapp:

  1. I can't contact a foreign mobile number without a country code - For example, if your phone number is from Croatia and you saved a contact with a mobile phone number from Austria, without the country code, Whatsapp will not recognize it as existing.
  2. I can contact domestic mobile number without country code - For example, if your phone number is from Croatia and you saved a contact with a mobile phone number from Croatia, without the country code, Whatsapp will recognize it as it is (existing).
  3. I can contact a foreign mobile number with "00" country code - For example, if your phone number is from Croatia and you saved a contact with a mobile phone number from Austria, with the 0043 country code standard, Whatsapp will recognize it as existing.
  4. I can contact a foreign mobile number with a "+" country code - For example, if your phone number is from Croatia and you saved a contact with a mobile phone number from Austria, with the +43 country code standard, Whatsapp will recognize it.
  5. I can contact a foreign mobile number with "+" country code and 0 before the mobile prefix - For example, save the foreign phone number in your contacts with "+" country code and zero before the mobile operator prefix (+385 0 99 1234567). Whatsapp will recognize and normalise the number to +385 99 1234567.
  6. I can't contact a foreign mobile number with "00" country code and 0 before mobile prefix - For example, save the foreign phone number in your contacts with "00385" country code and zero before mobile prefix (+385 0 99 1234567). Whatsapp will not recognize the number as the same as +385991234567.
  7. I can't contact domestic mobile number with "00" country code and 0 before mobile prefix - For example, save the domestic phone number in your contacts with "00385" country code and zero before mobile prefix (+385 0 99 1234567). Whatsapp will not recognize the number as the same as +385991234567.

Handling phone numbers, especially without normalisation can create a real mess in your application because many users in different countries use different phone number standards to create their contacts.

Feel free to share your experience and your test case examples in the comments section.

Top comments (2)

Collapse
 
dylanlacey profile image
Dylan Lacey

Normalisation is a really interesting topic, especially because I think there's a tendency for devs to assume that all data can ultimately be normalised.

Which is probably not true? Things with standards are almost normalised, providing the standards work for all valid data.... But data is validated by the originating jurisdiction, not the consumer.

This is kind of a pet peeve of mine; I actually gave a talk at (Automation Guild 2022)guildconferences.com/automation-gu... about bad testing data assumptions and how they interact with names and addresses. Most of my examples were about Japan, where, for example:

  • The characters representing a name may also represent other names
  • Any particular name may have multiple characters it can be spelt with
  • There are no middle names
  • A person may interact with a Western system using a chosen Western name that isn't theirs, legally
  • Not all streets have names
  • Building number may be non-sequential
  • Kyoto has a "citizen" addressing scheme... Which the government recognises as valid alongside the nationwide addressing scheme

There's more. A lot more. The upshot I have is that normalisation is a super useful tool when it's possible, but trying too hard for it leads too pain.

(Phone numbers tho; 100% normalise them)

Collapse
 
ajdinmust profile image
Ajdin Mustafić

Thanks for the so detailed and helpful comment, @dylanlacey ! I have watched you speak on this topic on Automation Guild this year, great talk.