2
1 Comment

Avoid these mistakes with phone numbers

Phone numbers are a complete mess. If you collect user phone numbers, you will encounter all sorts of local number formats that will make you scratch your head.

Turning them into one standard format is important to make them a searchable field in your database.

Here is a selection of different phone number formats people in different countries might use:

  • 123-456-7890
  • (234) 567-8901
  • 0345 / 678 9012
  • +456 789 0123
  • 567-890-1234

How on earth will you turn those into the standard format?

The standard

E.123 by the International Telecommunication Union defines the international phone number format as this:

+12 345 678 901

Ideally, you would store all your phone numbers in this format, but without spaces in your database. To be safe, I would also store the raw string that the user has entered.

Avoid these mistakes

Mistake #1: Don't ask the user

If you can, collect the phone number in the correct format. A library like International Telephone Input can help you do this. Its phone number field looks like this:

International Telephone Input

Mistake #2: Save the number as an integer

If you just strip all the special characters and store the phone number as an integer, you are in deep trouble. 007 and 07 and 7 are not the same phone numbers, but you will store them simply as 7. Store phone numbers as strings.

Mistake #3: Don't use libraries

Google has open-sourced their libphonenumber and you should check it out! There is also a JavaScript-port called google-libphonenumber.

Mistake #4: Just prepend a country code

What if for a specific phone number using a library fails for you? Then use all the context info you have and figure out the country code the user could have implied. At this point it may be wise to feed that country info into one of the libraries and let them try to normalize the phone number again.

Final Thoughts

If you avoid these four mistakes, you should end up with nice and standard phone numbers in your database. With this format you can later look up records by phone number without worrying about special cases.

I've published a longer version of this at How to untangle phone numbers in the FactBranch blog. There I also write about philosophical aspects and fun facts like non-ASCII characters in phone numbers or that numbering plans can change over time.

posted to Icon for group Developers
Developers
on May 9, 2024
  1. 2

    Phone numbers are tricky, especially when trying to validate them client-side in a SPA. I agree, everyone should use existing libraries, but the validation libraries are huge, so spend some time to think about how to load/bundle it if you want to do it on the frontend. When using those phone UI component libraries, the country indicators often load the flag icons from external services. As it's not recommended to include all those resources in the bundle, you may need to host the country indicators yourself (and configure the library to use your own hosted country indicators) if you operate in regions with strict data protection laws like the EU.

    Regarding mistake 4... There is a popular (measured by downloads) NPM library for phone numbers. If you check the source code, you will find that the library will automatically prepend the country code for India when not set by the user. So it's always worth checking the source code of libraries you use!

Trending on Indie Hackers
AI runs 70% of my distribution. The exact stack. User Avatar 148 comments I'm a solo founder. It took me 9 months and at least 3 stack rewrites to ship my SaaS. User Avatar 137 comments Show IH: I'm building a lead gen + CRM tool for web designers targeting local businesses without websites — starting with Spain User Avatar 79 comments I built a URL indexing SaaS in 40 days — here's the honest story User Avatar 58 comments We could see our AI bill, but not explain it — so I built AiKey User Avatar 25 comments AI coding should not turn software development into a black box User Avatar 13 comments