Skip to content

Mobile Number Validation – How To Do It Right?

I don’t know about you, but i get lots of e-mail everyday. I mean LOTS. Some of them are short reminders that reside in my inbox for days to actually remind me of something. If I could choose, I would prefer to receive reminding SMS. It’s more interruptive, but reminders should work this way :)

In the world there is nearly 5 billions mobile phone users. Because of that SMS becomes as effective communication medium as e-mail. It is significant to gather mobile phone number alltogether with e-mail address in your users database. In this article you’ll learn how to avoid traps along the way. I’ll show you what I’ve learned about mobile number, how to store it in database or what you can expect from user.

Why Should I Even Bother Asking for Mobile?

If you’re simply running business, notification about special offer sent via text message would be more direct than e-mail. If you have some kind of SaaS or different app, you can use SMS for two-factor authentication or recovering lost password. It’s crucial to have correct number in database for such occasion.

The worldwide smartphone market grew 13.0% year over year in 2015 Q2
The worldwide smartphone market grew 13.0% year over year in 2015 Q2

You can manage your customers data and numbers by hand in some Excel sheet, notebook or dedicated app. You can use dedicated software for this, or even your users can registrate by themselves. So your user can provide his number himself in some registration process. In all those cases you want to ensure that number is valid.

During registration you can depend on user’s input (it’s in their interest to provide correct number) but from the view of User Experience you should protect such input from mistakes. How you can validate mobile number?

Mobile Number Possible Formats

So let’s begin with mobile number format. Validation problem is not trivial as numbers are very country specific. Different lengths, country codes, even number prefix when calling within or outside country. Every number can be formatted in many ways, but most popular are national and international or even E164 format. Let’s consider fake US number:

  1. National format
    (202) 555-0100
  2. International format
    +1 202-555-0100
  3. E164 format
    +12025550100

You can see that each format slightly differs from the others. National format skips country code (as it is used within country) which is ‘+1’, ‘202’ part is area code and ‘5550100’ is number itself. Please note, that it can have also different separators than dashes and it would differ all over the world.

E164 format is simplest standard for managing numbers. There’s no spaces or other delimiters, and it’s best for storing in database. It can be easily formatted when viewing (I’ll show you later how) so it would be probably your best storage choice.

Now, your user has to provide mobile number in input form. How would you know which format he would use? How would you force one and only format for further maintenance?

Simplest Validation (or not?)

First thought would be: REGEX! If you need gathering numbers only for your country, you can try to build complex regex which would validate it correctly. But if you’re my reader, most probably you would like to have some global solution working all over the world.

As a big regex fan, I’m sure it is possible to create expression matching all mobile numbers globally. But to show you, how complicated it can get, here’s regex matching only US numbers according to StackOverflow:

Such code would match 7 or 10 digits number with extensions and dashes, periods or dots as delimiters.

But how would you build expression for all countries over the world? If it even would be possible, it would be an overkill to maintain it.

And it even gets worse in other countries. Country codes can have variable length (from 1 to 3 digits) and so can numbers, valid area codes are country specific and there’re some additional rules which number must follow in chosen countries.

Yup, regex is an overkill.

Let’s Send a Message!

So, next thought would be to send an SMS to number provided by user. Send him a code, to confirm that number is valid. Such code would assure that number is 100% correct. Sounds great!

Sending SMS confirmation code gives you 100% certainty that number is valid
Sending SMS confirmation code gives you 100% certainty that number is valid

But still, SMS costs you money. Even if it’s 0.01$, let 100,000 registrations have invalid mobile number for the first time, and you’ll send 100,000 * 0.01$ = 1,000$ in outer space. As user won’t receive message, he’ll probably catch typo but you get the point. Always try to simplify user’s life before he would make a mistake. He will be grateful :)

Maybe there’s other option?

Call Validating API!

So, for now we have one possibility – send text message to user. Most of SMS sending services would operate only on international number format. How to check if number is valid, so we can send authorization code?

There’s many SaaS platforms validating mobile phone numbers. So, before sending SMS you can call such API to check if number’s correct. Easy like calling API with number and receiving true/false as a result. Sounds like a great idea.

validate-mobile-number-notification
Mobile notifications are one of most effective ways to communicate with user

Some Software-as-a-Service SMS providers would have such validation built-in. So if you’ll try sending text message to invalid number – you’ll get response about invalid number and you won’t be charged for it.

Fair enough.

But still – if your user inputs some nonsense, he must wait for API response to see if it’s valid. It should be better to prevalidate number before trying to send anything. And from UX view it’d be great to skip all those sending part. Is it possible to know for 100% sure that number is correct?

Google libphonenumber For The Win!

Well, I cannot guarantee you 100%, but there’s solution which will give you 99% percent of certainty. It probably won’t surprise you, but Google had the same problem as you. They’re one of most internationalized companies and they struggled with the same – how to validate user’s mobile number.

So they created their own library – libphonenumber.

At first it was intended to be used on Android, but at this time it’s in almost all of modern programming languages: C++, Java, JavaScript, C#PHP, objective-C, Python and Ruby.

Basically, libphonenumber contains data about phone number rules (not only mobile) across the world. Google is managing it, and for me it’s assurance that data will be consistent and all errors will be caught as soon as possible.

So how to use it? Validation is pretty trivial:

As you can see, library easily can parse any number. In process, you’ll get object containing all knowledge about it. Apart from validation, you can check is it possible that it’s mobile. Sometimes you’ll get straight answer as MOBILE, in other cases it would be FIXED_LINE_OR_MOBILE. Try it out yourself on this JavaScript demo page and check what Google’s know about your number :)

After you validate number and you store it in database as E164, libphonenumber has a set of formatting function so your stored E164 would become human-readable again:

7 Best Tips for Handling Mobile Numbers

So, to sum this all up:

  1. That may seem obvious, but remember to always validate user input,
  2. Be aware of many different number formats,
  3. Stick to one format (E164 would be the best) when operating on numbers internally,
  4. Don’t even dare to build validating regex, there’s no need to,
  5. Use libphonenumber instead,
  6. Validate number trough SMS code if you need 100% certainty,
  7. Format numbers for display,

So that’s it, thank you for reading my article and reaching this far. If you like it, please share it with your programmer friends. At this point I hope that you’re convinced that you need validation and libphonenumber should be your first choice. Giants like Google are doing great job maintaining such global information.

In one of my next articles I’ll cover technicalities of implementing such well-validated form in HTML+JS. If you don’t want to miss it – subscribe to my newsletter. Once again, thank you for being my reader! If you struggle with anything in i18n – let me know, we’ll try to find answer together :)

PS: this week you can meet me on PHPCon Poland! I’ll be there whole saturday and few morning lectures on sunday (14 and 15 Nov 2015). It would be great even to just high five with you :)

[do action=”cc-image-attribution” author=”Johan Larsson” photourl=”https://www.flickr.com/photos/johanl/4412680795″ cclicense=”by”/]
[do action=”cc-image-attribution” author=”Clemens Löcker” photourl=”https://www.flickr.com/photos/98641062@N05/9399040707″ cclicense=”by-sa”/]