isFQDN function

bool isFQDN(
  1. String str, {
  2. bool requireTld = true,
  3. bool allowUnderscores = false,
})

check if the string str is a fully qualified domain name (e.g. domain.com).

  • requireTld sets if TLD is required
  • allowUnderscore sets if underscores are allowed

Implementation

bool isFQDN(
  String str, {
    /// sets if Top Level Domain is required
  bool requireTld = true,

      /// sets if underscores are allowed
  bool allowUnderscores = false,
}) {
  final parts = str.split('.');
  if (requireTld) {
    final tld = parts.removeLast();
    if (parts.length == 0 || !RegExp(r'^[a-z]{2,}$').hasMatch(tld)) {
      return false;
    }
  }

  for (final part in parts) {
    if (allowUnderscores) {
      if (part.contains('__')) {
        return false;
      }
    }
    if (!RegExp(r'^[a-z\\u00a1-\\uffff0-9-]+$').hasMatch(part)) {
      return false;
    }
    if (part[0] == '-' || part[part.length - 1] == '-' || part.indexOf('---') >= 0) {
      return false;
    }
  }
  return true;
}