Skip to content

Instantly share code, notes, and snippets.

@joepuzzo
Last active February 24, 2022 17:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joepuzzo/45e308e62fc9f2f821c8509017c450d8 to your computer and use it in GitHub Desktop.
Save joepuzzo/45e308e62fc9f2f821c8509017c450d8 to your computer and use it in GitHub Desktop.

Problem Statement

  • input formatting ( making it look correct )
  • input validation ( checking that the number is valid )

Standards

E.164

Terminology

Trunk Prefix

A trunk prefix is a digit sequence to be dialed before a telephone number to initiate a call for the purpose of selecting an appropriate telecommunications circuit by which the call is to be routed.

IDD is a trunk prefix NDD is a trunk prefix

————————————————————————————————————————

IDD ( International Direct Dialing )

Layman's Terms: How do I get out of my country !

Also known as: Exit Prefix, International Dialing Code, and International Call Prefix

Example: I want to get Out of the USA and into Australia:

011 61 7 3333 3333 
^^^

So the USA’s IDD is 011

Note: this can be replaced by + symbol when formatting a phone number to match E.164

011 61 7 3333 3333 ------> + 61 7 3333 3333

————————————————————————————————————————

Country Code

Layman's Terms: How do I get into a country !

Example: I want to get Into Australia from the USA:

011 61 7 3333 3333 
    ^^ 

————————————————————————————————————————

NDD ( National Direct Dialing )

Layman’s Terms: Extra numbers when dialing within a country

Background: in a number of countries, local dialing may require the addition of a '0' in front of the subscriber number. With E.164 formatting, this '0' must usually be removed.

Example:

Dial Within Australia: 07 3333 3333
					   ^

Dial Into Australia ( from US )	: 011 61 7 3333 3333

							^^^NO 0 in front of 7^^^

E.164 Format: + 61 7 3333 3333


Different formats

Why are phone numbers so hard to format?

The following examples of how people will type phone numbers in england.

International

Number Formatted Type
1212345678 +44 121 234 5678 landline in birmingham
2012345678 +44 20 1234 5678 landline in london
1525123456 +44 1525 123456 landline in Leighton Buzzard
1525123456 +44 1525 123 456 landline in Leighton Buzzard formatted differently
7400123456 +44 7400 123456 mobile
7400123456 +44 7400 123 456 mobile formatted differently

Domestic

Number Formatted Type
1212345678 0121 234 5678 landline in birmingham
2012345678 020 1234 5678 landline in london
1525123456 01525 123456 landline in Leighton Buzzard
1525123456 01525 123 456 landline in Leighton Buzzard formatted differently
7400123456 07400 123456 mobile
7400123456 07400 123 456 mobile formatted differently

Note: This library currently only supports the international ( E.164 ) formats.


Storage

Using the above, we break a phone number down into its components.

Given 011 61 07 3333 3333 you can get:

{
	"iddPrefix": "011",
	"countryCode": "61",
	"nddPrefix": "0",
	"areaCode": "7",
	"number": "33333333",
}

However whats required to be stored is:

{
	"country": "AU",
	"areaCode": "7",
	"number": "33333333",
}

The rest can be derived from the above

Also acceptable storage

{
	"country": "AU",
	"countryCode": "61",
	"number": "733333333",
}
{
	"country": "AU",
	"number": "733333333",
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment