Reputation: 889
I want to verify my users phone numbers using a service such as Nexmo or Twilo.
To send a message via these services I need to supply the international number, however, I do not want to have to ask my users to provide the full international number - just their normal number that they would give to anyone in their own country (I don't expect them to know their international dialling codes, etc).
So what is the best way to convert a local national number into an international one? As well as their number, each user will be asked to provide their country. (My user base will be international.)
Should I add an extra field to the Country table that holds the international dialling code - and then just construct the full international number? This seems ok until you consider that some countries require omitting the 0 at the start of the number, and others don't.
Is there an easier way? Or gem out there that does this?
Upvotes: 2
Views: 1372
Reputation: 33
The phony_rails gem provides international formatting and validation. This is a rails wrapper for phony giving you view helpers and model validators. To see all the countries it supports see the phony repository.
Upvotes: 0
Reputation: 652
Google’s opensource libphonenumber project is probably the best tool for any sort of work involving phone numbers. Given a country and phone number, libphonenumber can be used to produce the E.164 format which Twilio prefers. It can also produce example phone numbers to improve the accuracy of the data you collect in the first place. A ruby port is available. An example Twilio app using libphonenumber in php can be found in Twilio Skills training.
Upvotes: 3