When I built the free number checker on Plugg, I thought the hard part would be the scam reports. It wasn't. The hard part was turning whatever people type into one clean format, for 54 African countries.

People paste numbers the way they see them: 0803 123 4567, +234 803 123 4567, 234-803-1234567, 00234 803.... If you store all of those as different strings, your lookup misses half the matches. So everything has to become E.164: a plus, the country code, then the subscriber number, with no spaces.

The trap: the leading zero

In many countries, the zero at the start of a local number is a "trunk prefix". You dial it at home, but it is not part of the number. A Nigerian 0803 123 4567 becomes +234 803 123 4567, and the zero disappears.

In other countries there is no trunk zero, so you must not drop anything. Côte d'Ivoire is the classic gotcha: since its move to 10-digit numbers, the leading zero is part of the number itself, so stripping it breaks the number. Senegal and Cameroon numbers have no leading zero at all.

So the rule is not "always drop the zero". It is "drop the zero only in countries that use a trunk prefix".

A small function that handles it

const CODES = { NG: '234', GH: '233', KE: '254', ZA: '27', CI: '225', SN: '221', CM: '237' }; // Countries where a leading 0 is a trunk prefix (dialled locally, not part of the number) const TRUNK_ZERO = new Set([ 'NG','GH','KE','ZA','UG','TZ','RW','ZM','ZW','MW','EG','MA','DZ', 'ET','SD','SS','NA','CD','LY','ER','MG','LR','SL' ]); function toE164(input, iso) { const cc = CODES[iso]; let d = String(input).replace(/\D/g, ''); // keep digits only if (d.startsWith('00')) d = d.slice(2); // 00 international prefix if (d.startsWith(cc) && d.length > cc.length + 6) return '+' + d; // already has the code if (TRUNK_ZERO.has(iso) && d.startsWith('0')) d = d.slice(1); // drop trunk zero return '+' + cc + d; } toE164('0803 123 4567', 'NG'); // +2348031234567 toE164('+234 803 123 4567', 'NG'); // +2348031234567 toE164('024 123 4567', 'GH'); // +233241234567 toE164('07 07 12 34 56', 'CI'); // +2250707123456 (zero kept) 

Things I learned the hard way:

  • Strip everything that is not a digit first. Spaces, dashes, brackets and the plus sign all show up in pasted numbers.
  • Check for the country code before dropping zeros. A number that already starts with the code should pass through untouched.
  • Keep the country list in data, not in if statements. I generate one checker page per country from the same table, so adding a country is one line.
  • Validate length per country if you can. The function above is forgiving on purpose, because a scam checker should still search a slightly odd number rather than reject it.

Where it is used

Every country page on Plugg has its own checker, for example Nigeria, Ghana, Kenya and Côte d'Ivoire. The form runs a version of this function and then searches the reports for the clean number.

If you work with African phone numbers and know a country where my trunk-zero list is wrong, tell me in the comments. I would rather fix it than have someone miss a scam report.

I'm Bank, a self-taught developer in Enugu, Nigeria. I build Plugg Africa on my own.