isValidPhoneNumber static method

bool isValidPhoneNumber({
  1. required String isoCode,
  2. String? phoneNumber,
  3. bool isRequired = true,
})

Validates if the provided phone number matches the country's pattern.

isoCode: ISO code of the country (e.g., 'BR', 'US').

phoneNumber: Phone number to be validated.

isRequired: Whether the phone number is required. If true, an empty phone number will return false.

Returns true if the number is valid for the country, false otherwise.

Implementation

static bool isValidPhoneNumber({
  required String isoCode,
  String? phoneNumber,
  bool isRequired = true,
}) {
  if (isRequired && (phoneNumber == null || phoneNumber.isEmpty)) {
    return false;
  }

  try {
    final phone = PhoneNumber.parse(phoneNumber!,
        destinationCountry: IsoCode.fromJson(isoCode));

    return phone.isValid();
  } catch (e) {
    return false;
  }
}