Modules
Phone numbers
Read, check and format Madagascar (+261) phone numbers, mobile and fixed, written in any common way, and know the operator of each one.
At a glance
Section titled “At a glance”- +261Country code
- 34Operator prefix
- 12 345 67Subscriber number
- Operator
- Yas Madagascar
- Line
- mobile
Try it
Section titled “Try it”Result
+261 34 12 345 67 · Yas Madagascar
JSformatPhone('034 12 345 67', 'international')PHPPhoneNumber::parse('034 12 345 67')->format(PhoneNumberFormat::International)Quick reference
Section titled “Quick reference”parsePhone(input) | Text → parsed value, or throws |
safeParsePhone(input) | Text → parsed value or error code, never throws |
tryParsePhone(input) → undefined | Text → parsed value, or nothing, never throws |
isValidPhone(input) | Any value → true/false, never throws |
formatPhone(input, format) | Text → e164, international or national |
OPERATOR_NAMES[id] | Operator id → name, e.g. yas → Yas Madagascar |
PhoneNumber::parse($input) | Text → parsed value, or throws |
PhoneNumber::safeParse($input) | Text → parsed value or error code, never throws |
PhoneNumber::tryParse($input) → null | Text → parsed value, or nothing, never throws |
PhoneNumber::isValid($input) | Any value → true/false, never throws |
PhoneNumber::parse($input)->format($format) | Text → e164, international or national |
Operator::displayName() | Operator id → name, e.g. yas → Yas Madagascar |
Basic usage
Section titled “Basic usage”Spaces, dashes, dots, parentheses, +261, 00261 and the leading 0 are all accepted.
import { parsePhone } from '@mdgascar/phone-number';
parsePhone('034 12 345 67');// { e164: '+261341234567', nationalNumber: '341234567', operator: 'yas', type: 'mobile', area: null }
parsePhone('+261 32 12 345 67').operator; // 'orange'use Mdgascar\MalagasyIntl\PhoneNumber\PhoneNumber;
$phone = PhoneNumber::parse('034 12 345 67');$phone->e164; // '+261341234567'$phone->nationalNumber; // '341234567'$phone->operator; // Operator::Yas
PhoneNumber::parse('+261 32 12 345 67')->operator; // Operator::OrangeFixed lines
Section titled “Fixed lines”Fixed-line numbers are read too: 020 for the Yas (Telecom Malagasy) network and 022 for Gulfsat. A parsed number tells its type, and the town of a geographic number when the national plan lists it.
- +261Country code
- 20Operator prefix
- 22 421 19Subscriber number
- Operator
- Yas Madagascar
- Line
- fixed
- Town
- Antananarivo
parsePhone('020 22 421 19').type; // 'fixed'parsePhone('020 22 421 19').area; // { name: 'Antananarivo', province: 'Antananarivo' }parsePhone('034 12 345 67').type; // 'mobile'PhoneNumber::parse('020 22 421 19')->type; // PhoneLineType::FixedPhoneNumber::parse('020 22 421 19')->area?->name; // 'Antananarivo'PhoneNumber::parse('034 12 345 67')->type; // PhoneLineType::MobileFormatting
Section titled “Formatting”Three formats: e164 for storage and APIs, international for people abroad, national for people in Madagascar.
| Case | Call | Result |
|---|---|---|
| E.164 | formatPhone('0341234567', 'e164') | +261341234567 |
| international | formatPhone('0341234567', 'international') | +261 34 12 345 67 |
| national | formatPhone('0341234567', 'national') | 034 12 345 67 |
import { formatPhone } from '@mdgascar/phone-number';
formatPhone('034 12 345 67', 'e164'); // '+261341234567'formatPhone('034 12 345 67', 'international'); // '+261 34 12 345 67'formatPhone('+261341234567', 'national'); // '034 12 345 67'use Mdgascar\MalagasyIntl\PhoneNumber\PhoneNumber;use Mdgascar\MalagasyIntl\PhoneNumber\PhoneNumberFormat;
$phone = PhoneNumber::parse('034 12 345 67');$phone->format(PhoneNumberFormat::E164); // '+261341234567'$phone->format(PhoneNumberFormat::International); // '+261 34 12 345 67'$phone->format(PhoneNumberFormat::National); // '034 12 345 67'Operator names
Section titled “Operator names”import { OPERATOR_NAMES, parsePhone } from '@mdgascar/phone-number';
OPERATOR_NAMES[parsePhone('034 12 345 67').operator]; // 'Yas Madagascar'OPERATOR_NAMES.orange; // 'Orange Madagascar'PhoneNumber::parse('034 12 345 67')->operator->displayName(); // 'Yas Madagascar'Operator::Orange->displayName(); // 'Orange Madagascar'Checking user input
Section titled “Checking user input”isValid never throws. safeParse tells you why a number was rejected, tryParse gives you the number or nothing.
import { isValidPhone, safeParsePhone, tryParsePhone } from '@mdgascar/phone-number';
isValidPhone('031 12 345 67'); // false
const result = safeParsePhone('031 12 345 67');if (!result.success) result.error.code; // 'UNKNOWN_OPERATOR'
tryParsePhone('031 12 345 67'); // undefinedPhoneNumber::isValid('031 12 345 67'); // false
$result = PhoneNumber::safeParse('031 12 345 67');$result->error?->errorCode; // ErrorCode::UnknownOperator
PhoneNumber::tryParse('031 12 345 67'); // nullErrors
Section titled “Errors”| Input | Error code |
|---|---|
| Not a string | INVALID_TYPE |
| Not a Malagasy phone number (wrong length, letters) | INVALID_FORMAT |
Prefix not assigned to an operator (031) |
UNKNOWN_OPERATOR |
Rules and sources
Section titled “Rules and sources”Madagascar phone numbers: rules and sourcesAccepted spellings, operator prefixes and the regulator sources.