isPhoneValid function

bool isPhoneValid(
  1. String phone, {
  2. bool allowEndlessPhone = false,
  3. String? defaultCountryCode,
})

Implementation

bool isPhoneValid(
  String phone, {
  bool allowEndlessPhone = false,
  String? defaultCountryCode,
}) {
  phone = toNumericString(
    phone,
    allowHyphen: false,
    errorText: null,
    allowAllZeroes: true,
  );
  if (phone.isEmpty) {
    return false;
  }
  PhoneCountryData? countryData;
  if (defaultCountryCode != null) {
    countryData = PhoneCodes.getPhoneCountryDataByCountryCode(
      defaultCountryCode,
    );
  } else {
    countryData = PhoneCodes.getCountryDataByPhone(phone);
  }
  if (countryData == null) {
    return false;
  }
  final cMask = countryData.getCorrectMask(defaultCountryCode);
  final cAltMasks = countryData.getCorrectAltMasks(defaultCountryCode);
  var formatted = _formatByMask(
    phone,
    cMask,
    cAltMasks,
    0,
    allowEndlessPhone,
  );
  final preProcessed = toNumericString(
    formatted,
    allowHyphen: false,
    errorText: null,
    allowAllZeroes: true,
  );
  if (allowEndlessPhone) {
    var contains = phone.contains(preProcessed);
    return contains;
  }
  var correctLength = formatted.length == cMask.length;
  if (correctLength != true && cAltMasks != null) {
    return cAltMasks.any(
      (altMask) => formatted.length == altMask.length,
    );
  }
  return correctLength;
}