IPv4 / IPv6 ValidateLoading saved progress…

IPv4 / IPv6 Validate

An IP address is the label that identifies a machine on a network, and it comes in two formats you have to tell apart: IPv4, four decimal numbers joined by dots like 192.168.1.1, and IPv6, up to eight groups of hexadecimal digits joined by colons like 2001:db8::1. You will build a small validator, ipAddressValidate = { isIPv4, isIPv6, version }, that decides whether a string is a well-formed address of either kind. The catch is in the details: an IPv4 octet runs 0255 with no leading zeros, and IPv6 lets a run of all-zero groups collapse into a single ::. See IPv4 and IPv6 on MDN for background.

Signature

ipAddressValidate.isIPv4(str)   // string -> boolean
ipAddressValidate.isIPv6(str)   // string -> boolean
ipAddressValidate.version(str)  // string -> 4 | 6 | 0

version returns 4 for a valid IPv4 address, 6 for a valid IPv6 address, and 0 for anything else.

Examples

ipAddressValidate.isIPv4('192.168.1.1');   // true
ipAddressValidate.isIPv4('256.0.0.1');     // false — 256 is out of range
ipAddressValidate.isIPv4('01.2.3.4');      // false — leading zero
ipAddressValidate.isIPv6('::1');                       // true
ipAddressValidate.isIPv6('2001:db8::8a2e:370:7334');   // true
ipAddressValidate.isIPv6('1:2:3:4:5:6:7:8:9');         // false — nine groups
ipAddressValidate.version('10.0.0.1');   // 4
ipAddressValidate.version('fe80::');     // 6
ipAddressValidate.version('nope');       // 0

Notes

  • IPv4 octets — exactly four parts split on ., each all digits, valued 0 to 255, with no leading zero: 0 is fine, 01 is not.
  • IPv6 groups — up to eight groups of one to four hexadecimal digits (09, af, case-insensitive) split on :. Leading zeros inside a group are allowed, so 0db8 is fine.
  • The :: shorthand — one :: may replace a run of one or more all-zero groups, and it may appear at most once. :: alone is the all-zeros address; ::1 and fe80:: are valid.
  • Parse the characters, not the numberNumber('1e2') is 100 and Number('') is 0, so numeric coercion cannot validate an octet. Check the digits yourself.
  • Out of scope — no CIDR suffixes like /24, no port numbers, and no IPv4-mapped IPv6 like ::ffff:192.0.2.1. Assume the input is a bare address string.

FAQ

Why can't I just use Number() to check an IPv4 octet?
Number() is built to be permissive: it reads '1e2' as 100, '0x1f' as 31, trims surrounding whitespace, and turns the empty string into 0. A validator needs the opposite, so you must confirm every character is a digit yourself and only then read the numeric value.
How does the :: shorthand work in IPv6?
A single :: replaces one or more consecutive all-zero groups so you do not have to type them out. It may appear at most once in an address, because a second :: would make it impossible to tell how many zero groups each one stands for.
Why are leading zeros banned in IPv4 but allowed in IPv6?
IPv4 octets are decimal and a leading zero (01) historically implied octal, so it is disallowed to avoid ambiguity. IPv6 groups are hexadecimal and fixed-width by convention, so a leading zero like 0db8 is just padding and is perfectly valid.
Loading editor…