Skip to main content

Validate IP Address

MediumPremium

Validate an IP address (IPv4). An address is valid if and only if it is in the form "X.X.X.X", where each X is a number from 0 to 255.

For example, "12.34.5.6", "0.23.25.0", and "255.255.255.255" are valid IP addresses, while "12.34.56.oops", "1.2.3.4.5", and "123.235.153.425" are invalid IP addresses.

Examples

ip = '192.168.0.1' output: true ip = '0.0.0.0' output: true ip = '123.24.59.99' output: true ip = '192.168.123.456' output: false

How many periods ('.') must the IP address have?

Try splitting the string into chunks delimited by the period character ('.').

For each chunk delimited by a period, how many possible strings are there?

Remember to check for leading 0s! For example, 192.168.001.001 is functionally equivalent to 192.168.1.1, but the leading zeros can cause confusion and are generally avoided in IPv4 addresses.

Does your code handle addresses like 008.123.34.56 appropriately?