phoneValidator function

String? phoneValidator(
  1. String? value
)

Validates a phone number.

Returns an error message if the phone number is invalid, based on the following rules:

  • If the value is null or empty, returns "Kolom tidak boleh kosong" (in Indonesian) or "Field can't be empty" (in English).
  • If the value does not start with '08', returns "Nomor harus berawalan '08', contoh: 08xxx" (in Indonesian) or "Phone numbers start with '08', e.g: 08xxx" (in English).
  • If the value has less than 10 characters, returns "Masukan minimal 10 karakter" (in Indonesian) or "Please fill in at least 8 characters" (in English).

Returns null if the phone number is valid.

Implementation

String? phoneValidator(String? value) {
  if (value == null || value.isEmpty) {
    return getCurrentLocalization() == 'id'
        ? 'Kolom tidak boleh kosong'
        : "Field can't be empty";
  } else if (!value.startsWith('08')) {
    return getCurrentLocalization() == 'id'
        ? "Nomor harus berawalan '08', contoh: 08xxx"
        : "Phone numbers start with '08', e.g: 08xxx";
  } else if (value.length < 10) {
    return getCurrentLocalization() == 'id'
        ? 'Masukan minimal 10 karakter'
        : 'Please fill in at least 8 characters';
  }
  return null;
}