validPhoneNumber static method

bool validPhoneNumber(
  1. String? phone, {
  2. String countryCode = 'vn',
})

Implementation

static bool validPhoneNumber(String? phone, {String countryCode = 'vn'}) {
  if (phone == null) return false;
  String checkPhone = phone;
  if (checkPhone.startsWith('+84')) {
    checkPhone = checkPhone.replaceAll('+84', '0');
  }
  if (checkPhone.startsWith('84')) {
    checkPhone = checkPhone.replaceAll('84', '0');
  }

  if (checkPhone.length != 10 && countryCode != 'vn') return false;
  bool found = false;
  for (CoutryPhoneNumber s in Common.listCoutryPhoneNumber) {
    if (s.countryCode == countryCode) {
      for (String ss in s.listStartPhoneHeader) {
        if (!checkPhone.startsWith('0')) {
          checkPhone = '0$checkPhone';
        }
        if (checkPhone.startsWith(ss)) {
          found = true;
          break;
        }
      }
    }
  }
  if (!found) return false;

  var pattern = '[0-9]{10}\$';

  var regex = RegExp(pattern);
  if (regex.hasMatch(checkPhone)) {
    return true;
  } else {
    return false;
  }
}