isIpAddress static method

bool isIpAddress(
  1. String s
)

Implementation

static bool isIpAddress(String s) {
  // Check IPv4 dotted notation

  // Shortes "0.0.0.0" = 7, longest "255.255.255.255" = 15
  if (isEmpty(s) || s.length < 7 || s.length > 15) {
    return false;
  }
  //Check regex
  RegExp regExp = new RegExp(r"^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$",
      caseSensitive: false, multiLine: false);
  if (!regExp.hasMatch(s)) {
    return false;
  }
  ;
  // Check range of all fields
  // Split string
  List<String> octets = s.split('.');
  // Check range from 0 to 255 of each octet
  for (String octet in octets) {
    if (int.parse(octet) < 0 || int.parse(octet) > 255) {
      return false;
    }
  }
  return true;
}