0

I am developing an Ionic app, and I need to validate an input field that may accept either a phone number, an email or a username. However, my application should be prepared to accept phone numbers from all over the world, which means that I should have a sort of generic solution to guarantee I am sending a valid number to a server.

I was checking how Facebook accept different phone formats to log in and I would like to make something similar. So I was wondering how I could achieve such thing. It allows me to log in using three different formats:

  • 0 (11) 9 1234-5678 when typing 011912345678
  • (11) 9 1234-5678 when typing 11912345678
  • 55 (11) 9 1234-5678 when typing 5511912345678

But as I am in Brazil, I believe it is easier to control it using a RegEx to validate only brazilian format numbers. But, how could I validate different phone numbers when I don't know where the user is? I'd like to hear from you guys if someone has achieved a solution for such problem. Thanks!

Edit: The main problem here is not using a RegEx to validate an international phone number format itself, but, how can I guarantee that even though the user may not type his International Country Code I still allow him to log in if he uses only his central office codes plus his subscriber number?

Caike Motta
  • 193
  • 3
  • 16
  • 1
    Short answer - there isn't really a single, meaningful RegEx for this. Long answer: Use Google's Phone Number library: https://github.com/googlei18n/libphonenumber - For a JS example, see here: http://rawgit.com/googlei18n/libphonenumber/master/javascript/i18n/phonenumbers/demo-compiled.html – Zhaph - Ben Duguid Nov 07 '17 at 13:22
  • @mplungjan I live in Brazil, and I used valid brazilian phone numbers. But this app is going to be used in other countries as well, and that's the problem, as I need a way to be sure I am sending a valid number. – Caike Motta Nov 07 '17 at 13:27

0 Answers0