Search code examples
twiliosmscountry-codes

How do I prompt a user for their telephone country code?


There are 6 countries that use 1 as their country code, the US & Canada as well as a number of Caribbean countries. (The only other dup AFAIK is RU/KZ.) I need to prompt a user for their country code & phone number.

I know this is a broad question, I am getting user's numbers to send them SMS alerts (via Twilio). Will I ever need to know if someone is in Canada vs. the US (or Trinidad & Tobago)? Or is all I truly need to know the +1?

In other words, can I store just the +1, or do I need to instead store US? Or even more of a PITA, store [1,US]?


Solution

  • I suggest storing the phone number in E164 format. Do your clean-up and validation of the number and then store in the database as a valid E164. You could separately ask your end user for their mailing address if the address matters to you. Also, I wouldn't bother trying to use regex to clean-up or validate phone numbers. Use the Twilio Lookup Api which is free to format numbers and will give you the country code, country, and you can trap exceptions for invalid numbers.

    E164 is consistent and will take care of the country code issue when you are calling or sending SMS.

    According to Twilio: "E.164 is the international telephone numbering plan that ensures each device on the PSTN has globally unique number.

    This number allows phone calls and text messages can be correctly routed to individual phones in different countries. E.164 numbers are formatted [+] [country code] [subscriber number including area code] and can have a maximum of fifteen digits."

    This page has more info "What is E.164?"