JavaScript: IPv6 validator

Last update: 02 Apr 23:20
0
Students

validator.js

Implement and export as default the predicate function isValidIPv6() that checks if IPv6 addresses (the sixth version of the Internet Protocol) are valid. The function takes a string with an IPv6 address as input and returns true if the address is valid, otherwise false.

Additional conditions:

  • The function does not depend on the case of characters
  • Leading zeros in digit groups are optional
  • The longest sequence of groups of zeros, for example, :0:0:0: can be replaced with two colons ::. Such a substitution can be made only once
  • One group of zeros :0: cannot be replaced by ::

Examples

isValidIPv6('10:d3:2d06:24:400c:5ee0:be:3d'); // true
isValidIPv6('0B0:0F09:7f05:e2F3:0D:0:e0:7000'); // true
isValidIPv6('000::B36:3C:00F0:7:937'); // true
isValidIPv6('::1'); // true

isValidIPv6('2607:G8B0:4010:801::1004'); // false
isValidIPv6('1001:208:67:4f00:e3::2c6:0'); // false
isValidIPv6('2.001::'); // false
isValidIPv6('9f8:0:69S0:9:9:d9a:672:f90d'); // false

Tips

  • IPv6
  • To check for borderline cases, carefully examine the list of IP addresses in the file with tests

For full access to the challenge you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests