isValidPhone static method

String? isValidPhone(
  1. String? phoneNo
)

Implementation

static String? isValidPhone(String? phoneNo) {
  if (phoneNo == null || phoneNo.isEmpty) {
    return 'Please enter a valid phone number.';
  }
  final compact = phoneNo.replaceAll(RegExp(r'\s+'), '');
  final regExp = RegExp(
    r'^\+?[1-9]\d{1,14}$',
  );
  if (!regExp.hasMatch(compact)) {
    return 'Please enter a valid phone number';
  }
  // With a country code the country's own numbering plan decides; without one there is nothing to check it against.
  if (compact.startsWith('+') && !PhoneParts.isValid(compact)) {
    return 'Please enter a valid phone number';
  }
  return null;
}