Let's say we have a registration system that finalises the creation of an account by SMSing the user their new account details.

This system could be used by people all around the world, so let's say we're using an online API that can send SMSs internationally regardless of the recipient's carrier. Because we want to maximise the use of this system, we would obviously need to cater for the different ways that different countries represent phone numbers: (example handily lifted from http://stdcxx.apache.org/doc/stdlibug/26-1.html)

754-3010,         Local number 
(541) 754-3010,   Domestic number
+1-541-754-3010,  International
1-541-754-3010,   Dialed in the US
001-541-754-3010, Dialed from Germany
191 541 754 3010, Dialed from France

What's the best way to represent the inputted phone number on submission to an API? Trivially (based on my own local representation in Australia) I might think:

{   "nationCode" : "+61",   "number" : "0400100200" }

But as the above examples show, different regional representations might not guarantee an international audience will type something on-screen the way I would.

Having recently come across libphonenumber, it seems like a better idea might be to accept a raw string as represented by the user, but also capture the country that the user's number is from. With these two pieces of information, the library appears to be able to do to the trick:

  PhoneNumber swissNumberProto = phoneUtil.parse(swissNumberStr, "CH");

Does anyone have any better suggestions on how to solve this problem? I realise that yes, it's possible to maximise usability for a user and let them type what they want - but the tradeoff there is the manual work that would be required to fix up bad numbers; and if the system is dependent on valid numbers in order to send the SMS, manual intervention is clearly a high price to pay.


An additional complication you will have to handle is that the way you dial the number may vary if you're calling from inside or outside the country. A UK number is typically written in the form 04321 123456 or (04321) 123456. When writing it as an international number, that would become +44 (0)4321 123456.

What is not obvious is that the leading 0 should not be dialled from outside the UK, but should be from inside the UK.

|improve this answer|||||

Its a complicated problem. Not only do you have differences in each countries phone number length and format. But also different ways of expressing that number with brackets and hyphens etc.

You can see from the google library that they have data files per carrier, which presumably have to be maintained.

Having recently tried to register for an account on the Indian Railways ticket booking system without having an Indian telephone number; I would advise you to just take accept string as typed and not force your customers to have to tweet you to get their account activated.

|improve this answer|||||
  • I agree that it's important to have a fallback; in my specific case (which is not an account registration system, it just makes a healthy example) we fall back to email notification if the SMS fails. It does strike me as an interesting problem though, especially when you see the number of sites that use mobiles for 2FA these days. – f1dave Mar 19 '18 at 2:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.