DEV Community

Bukunmi Odugbesan
Bukunmi Odugbesan

Posted on

Coding Challenge Practice - Question 119

The task is to implement a function that validates IPv4 and IPv6 IP addresses.

The boilerplate code

function isValidIP(str) {
  // your code here
}
Enter fullscreen mode Exit fullscreen mode

A valid IPv4 address should be in 4 parts, separated by ".", and should contain digits between 0 and 255, with no number having a leading zero. Create a function to validate those parameters

function isValidIPv4(ip) {
}
Enter fullscreen mode Exit fullscreen mode

Each part should be separated by "."

const parts = ip.split(".");
Enter fullscreen mode Exit fullscreen mode

There must be exactly 4 parts

if(parts.length !== 4) return false;
Enter fullscreen mode Exit fullscreen mode

If the number is less than zero or greater than 255, it is not a valid IPv4 address. The number should also not have a leading 0.

for (const part of parts) {
    if (!/^\d+$/.test(part)) return false;
    if (part.length > 1 && part[0] === '0') return false;

    const num = Number(part);
    if (num < 0 || num > 255) return false;
  }
  return true;
}
Enter fullscreen mode Exit fullscreen mode

The complete function to validate an IPv4 address

function isValidIPv4(ip) {
  const parts = ip.split('.');
  if (parts.length !== 4) return false;

  for (const part of parts) {
    if (!/^\d+$/.test(part)) return false;
    if (part.length > 1 && part[0] === '0') return false;

    const num = Number(part);
    if (num < 0 || num > 255) return false;
  }
  return true;
}
Enter fullscreen mode Exit fullscreen mode

A valid IPv6 address is exactly 8 parts, separated by ":". Each part contains 1 to 4 characters in hex digits. Create a function to validate these parameters.

function isValidIPv6(ip) {
}
Enter fullscreen mode Exit fullscreen mode

Each part must be separated by ":"

const parts = ip.split(":");
Enter fullscreen mode Exit fullscreen mode

There must be exactly 8 parts

if(parts.length !== 8) return false;
Enter fullscreen mode Exit fullscreen mode

If the part has fewer than 1 or more than 4 characters, it is invalid. The characters should also follow the hex code

for (const part of parts) {
    if (part.length < 1 || part.length > 4) return false;
    if (!/^[0-9a-fA-F]+$/.test(part)) return false;
  }
Enter fullscreen mode Exit fullscreen mode

The complete function to validate an IPv6 address.

function isValidIPv6(ip) {
  const parts = ip.split(':');
  if (parts.length !== 8) return false;

  for (const part of parts) {
    if (part.length < 1 || part.length > 4) return false;
    if (!/^[0-9a-fA-F]+$/.test(part)) return false;
  }
  return true;
}
Enter fullscreen mode Exit fullscreen mode

The address could either be an IPv4 or IPv6 address. If it is an IPv4 address, it would contain "."

if(str.includes('.')) {
    return isValidIPv4(str) ? true : false;
  }
Enter fullscreen mode Exit fullscreen mode

If it is an IPv6 address, it would contain ":"

if(str.includes(':')) {
    return isValidIPv6(str) ? true : false;
  }
Enter fullscreen mode Exit fullscreen mode

"." and ":" are the first differentiators, before the address are further validated with either function. The final code

function isValidIP(str) {
  // your code here
  if(str.includes('.')) {
    return isValidIPv4(str) ? true : false;
  }

  if(str.includes(':')) {
    return isValidIPv6(str) ? true : false;
  }
  return false;
}

function isValidIPv4(ip) {
  const parts = ip.split('.');
  if(parts.length !== 4) return false;

  for(const part of parts) {
    if(!/^\d+$/.test(part)) return false;
    if(part.length > 1 && part[0] === '0') return false;

    const num = Number(part);
    if(num < 0 || num > 255) return false;
  }
  return true;
}

function isValidIPv6(ip) {
  const parts = ip.split(':');
  if(parts.length !== 8) return false;

  for(const part of parts) {
    if(part.length < 1 || part.length > 4) return false;
    if(!/^[0-9a-fA-F]+$/.test(part)) return false;
  }
  return true;
}
Enter fullscreen mode Exit fullscreen mode

That's all folks!

Top comments (0)