formatPhoneInput function

String formatPhoneInput(
  1. String value
)

Formats a Pakistani mobile number as the user types: 0300-1234567, or +92 300 1234567 when the user starts with the country code.

Implementation

String formatPhoneInput(String value) {
  final split = _splitPhone(value);
  if (split == null) return value;
  if (split.incomplete) {
    return toAsciiDigits(value).replaceAll(_separators, '');
  }

  final digits = _take(split.national.replaceAll(_nonDigit, ''), 10);
  if (split.international) {
    return digits.length <= 3
        ? jsTrimEnd('+92 $digits')
        : '+92 ${digits.substring(0, 3)} ${digits.substring(3)}';
  }
  final local = '0$digits';
  return local.length <= 4
      ? local
      : '${local.substring(0, 4)}-${local.substring(4)}';
}