Skip to main content

Overview

LocalRegex provides a powerful formatNumber() extension method on String that allows you to format Zimbabwean phone numbers in different styles. This method validates the phone number and returns it in your preferred format.
The formatNumber() method is specifically designed for Zimbabwean numbers (mobile, VOIP, and landline). It validates against Zimbabwe-specific patterns before formatting.

Method Signature

String? formatNumber({
  required FormatType formatType,
  bool cleanNumber = true,
})

Parameters

formatType
FormatType
required
The desired output format for the phone number. See Format Types below.
cleanNumber
bool
default:"true"
Whether to strip all non-digit characters before formatting. When true, removes spaces, parentheses, hyphens, and other formatting characters.Examples:
  • (077) 612-30980776123098
  • (024) 212-30980242123098

Format Types

The FormatType enum provides three formatting options:

FormatType.regular

The standard local format with leading zero (e.g., 0777213388).This is the most common format used within Zimbabwe for mobile and landline numbers.
FormatType.regular

Usage Examples

import 'package:localregex/localregex.dart';

// Format to regular format
String? number = '+263777213388'.formatNumber(
  formatType: FormatType.regular
);
print(number); // Output: 0777213388

// Format to country code
String? number2 = '0777213388'.formatNumber(
  formatType: FormatType.countryCode
);
print(number2); // Output: 263777213388

// Format to country code with plus
String? number3 = '0777213388'.formatNumber(
  formatType: FormatType.countryCodePlus
);
print(number3); // Output: +263777213388

Before & After Examples

Input

'+263777213388'
'0777213388'
'263777213388'
'(077) 721-3388'

Output (Regular)

'0777213388'
'0777213388'
'0777213388'
'0777213388'

Input

'0777213388'
'+263777213388'
'(077) 721-3388'

Output (Country Code Plus)

'+263777213388'
'+263777213388'
'+263777213388'

Exception Handling

The formatNumber() method throws a LocalRegexException if the phone number is invalid:
import 'package:localregex/localregex.dart';

try {
  String? formatted = '1234567'.formatNumber(
    formatType: FormatType.regular,
  );
  print(formatted);
} on LocalRegexException catch (e) {
  print('Error: ${e.message}');
  // Output: Error: Phone number is not valid!
}
Always wrap formatNumber() calls in try-catch blocks when dealing with user input to handle invalid phone numbers gracefully.

Validation

The method automatically validates that the phone number matches one of these Zimbabwe patterns:
  • Mobile numbers: Econet, NetOne, or Telecel
  • VOIP numbers: Zimbabwe VOIP providers
  • Landline numbers: Zimbabwe landlines
If the number doesn’t match any of these patterns, a LocalRegexException is thrown.

Validation Methods

Learn about phone number validation methods

National ID Formatting

Format Zimbabwean national IDs

Build docs developers (and LLMs) love