extFormatPhoneNumber method

String extFormatPhoneNumber()

Implementation

String extFormatPhoneNumber() {
  if (this == null) return '';

  //* Menghapus semua karakter non-digit dari string
  String digitsOnly = this!.replaceAll(RegExp(r'\D+'), '');

  //* Memeriksa apakah nomor memiliki format yang sesuai
  if (digitsOnly.length < 12) {
    throw const FormatException('Nomor telepon tidak valid');
  }

  //* Mengambil bagian-bagian nomor telepon
  String countryCode = digitsOnly.substring(0, 2);
  String areaCode = digitsOnly.substring(2, 5);
  String firstPart = digitsOnly.substring(5, 9);
  String secondPart = digitsOnly.substring(9);

  //* Menggabungkan bagian-bagian nomor telepon dengan tanda hubung
  return '+$countryCode $areaCode-$firstPart-$secondPart';
}