annaoomph
annaoomph

Reputation: 592

The right way to search phone numbers

In my application, I search a user by his phone number on a server. I have encountered such an issue: in my country, you can write a phone number both as '8xxxxxxxx' and '+7xxxxxxxx' and '7xxxxxxxx'. So I should perform a search so that a number written in different notation still would be found. But how is it in different countries? How the numbers are written? Is there a way to perform a valid search?

Upvotes: 1

Views: 429

Answers (1)

Reez0
Reez0

Reputation: 2687

You'll probably want to look at the E.164 Standard Format.

E.164 organizes phone numbers with all the necessary localization information in a easily human readable and machine parseable format. It defines a simple format for unambiguously storing phone numbers in an easily readable string. The string starts with a + sign, followed by the country code and the “subscriber” number which is the phone number without any context prefixes such as local dialing codes, international dialing codes or formatting.

Numbers stored as E.164 can easily be parsed, formatted and displayed in the appropriate context, since the context of a phone number can greatly affect its format.

The library responsible for this is called Google’s libphonenumber. With libphonenumber you can parse, verify, and format phone number inputs quite easily, do as you type formatting and even glean extra information about the number, like whether it was a mobile or landline or what state or province it was from. Libphonenumber in its basic form consists of a set of rules and regular expressions in an XML file for breaking down and parsing a number.

Upvotes: 1

Related Questions