Skip to main content

Overview

LocalRegex provides a formatID() extension method on String that formats Zimbabwean national identification numbers into the official format used on government-issued ID cards.

Method Signature

String? formatID({
  IdFormatType formatType = IdFormatType.proper
})

Parameters

formatType
IdFormatType
default:"IdFormatType.proper"
The desired output format for the national ID. See Format Types below.

Format Types

The IdFormatType enum provides two formatting options:

IdFormatType.proper

The official format as seen on Zimbabwean national identity cards.Format: XX-XXXXXX X XXExample: 11-223344 K 55This is the default format and matches the layout on physical ID documents issued by the Zimbabwean government.
IdFormatType.proper

Usage Examples

import 'package:localregex/localregex.dart';

// Format to proper format (default)
String? formatted = '11223344K55'.formatID(
  formatType: IdFormatType.proper
);
print(formatted); // Output: 11-223344 K 55

Format Breakdown

The proper format 11-223344 K 55 consists of:
1

Prefix (2 digits)

The first two digits identify the registration district.Example: 11
2

Hyphen Separator

A hyphen separates the district from the registration number.Example: -
3

Registration Number (6 digits)

Six digits representing the sequential registration number.Example: 223344
4

Check Letter (1 character)

A single uppercase letter used as a check digit.Example: K
5

Suffix (2 digits)

Two final digits with a space before the last digit.Example: 55 becomes 5 (space) + 5

Before & After Examples

Input

'11223344K55'
'11-223344K55'
'11 223344 K 55'
'11-223344 K55'

Output (Proper)

'11-223344 K 55'
'11-223344 K 55'
'11-223344 K 55'
'11-223344 K 55'

Input

'11-223344 K 55'
'11 223344 K 55'
'11223344K55'

Output (No Space)

'11223344K55'
'11223344K55'
'11223344K55'

Exception Handling

The formatID() method throws a LocalRegexException if the national ID is invalid:
import 'package:localregex/localregex.dart';

try {
  String? formatted = 'INVALID123'.formatID();
  print(formatted);
} on LocalRegexException catch (e) {
  print('Error: ${e.message}');
  // Output: Error: National ID is not valid
}
Always validate user input with try-catch blocks when formatting national IDs to handle invalid IDs gracefully.

Case Handling

The formatID() method automatically converts the check letter to uppercase, ensuring consistent output regardless of input casing.
// Lowercase input
String? id1 = '11223344k55'.formatID();
print(id1); // Output: 11-223344 K 55

// Mixed case input
String? id2 = '11-223344 k 55'.formatID();
print(id2); // Output: 11-223344 K 55

Validation

The method validates the national ID using LocalRegex.isZimID() before formatting. The ID must:
  • Match the Zimbabwean national ID pattern
  • Contain valid district prefix
  • Have the correct structure with check letter
  • Include proper suffix digits
If validation fails, a LocalRegexException is thrown with the message: "National ID is not valid".

Use Cases

User Forms

Display formatted IDs in registration and profile forms for better readability.

Database Storage

Use IdFormatType.noSpace for consistent, space-free storage in databases.

Document Generation

Format IDs in the proper government format for official documents and reports.

Data Import

Standardize ID formats when importing data from various sources.

National ID Validation

Learn about national ID validation methods

Phone Number Formatting

Format Zimbabwean phone numbers

Build docs developers (and LLMs) love