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

199 Views Asked by At

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]?

2

There are 2 best solutions below

2
jassent On BEST ANSWER

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?"

0
Kaelyn Chresfield On

If you plan to send SMS messages outside of your home country there are a few things to consider:

From the Twilio support docs, "While Twilio supports international SMS to 235 destinations, please keep in mind that each country has its own individual regulations and guidelines that may apply based on sender type, use case, or may require additional registration."

I recommend documenting the receiver's country to:

  • Document pricing to send SMS messages
  • See how and if Twilio supports A2P (application to person) SMS messaging in each country
  • Research the regulatory and compliance guidelines (such as MMS, use case restrictions, etc) in each country to and make sure your messages are compliant with local laws
  • Update the geo-permissions in your console to include the customer's country

To find the regulatory and compliance information for each supported country go to:

https://www.twilio.com/en-us/guidelines/{ISO-country-code}/sms

A list of ISO country codes can be found here

I also recommend checking out Twilio's messaging policy and acceptable use policy to make sure Twilio isn't filtering your messages before they're ever sent. Especially if you're sending bulk SMS alerts.

Please comment if you have any further questions or need additional resources!