This post covers various methods to validate an IP address in Java.

An IP address in IPv4 is defined as a 32-bit number and usually represented in dot-decimal notation, consisting of four decimal numbers separated by dots, each ranging from 0 to 255, such as 172.68.58.63.

IPv6 is the successor to the IPv4 and is defined using 128 bits (or 16 octets) such as 2405:204:638b:9daa:f3c8:a903:3227:c712. Because of the historical prevalence of IPv4, the generic term IP address typically still refers to the addresses defined by IPv4.

In this post, we’ll validate IPv4 addresses by checking that given addresses consists of exactly four “decimal” numbers ranging between 0 and 255, separated by dots, i.e., the valid IP address format is xxx.xxx.xxx.xxx, where xxx lies between 0 and 255. Please note that octal or hexadecimal numbers are not permitted, i.e., 0xx, 0x, 0xA.

 
For example, 1.1.1.1, 127.0.0.1, 255.255.255.255, 172.68.8.63 are valid IPv4 addresses and the following IPv4 addresses are invalid:

1.1.1 (only 3 decimal numbers)
1.1.1. (ending with a dot)
1.1..1 (two consecutive dots)
.1.1.1 (starting with a dot)
1.1.1.x (containing an alphabet)
172.8.9.256 (decimal number exceeds 255)
172.8.-9.255 (negative decimal number)
172.8.9.266 (decimal number exceeds 266)
172.013.1.2 (contains octal number 013)
172.a.1.2 (contains hexadecimal number a)
and many more…

1. Using Apache Commons Validator

Apache Commons Validator package contains several standard validation routines. We can use InetAddressValidator class that provides the following validation methods to validate an IPv4 or IPv6 address.

  1. isValid(inetAddress): Returns true if the specified string is a valid IPv4 or IPv6 address.
  2. isValidInet4Address(inet4Address): Returns true if the specified string is a valid IPv4 address.
  3. isValidInet6Address(inet6Address): Returns true if the specified string is a valid IPv6 address.

The following program demonstrates it:

Download Code

Output:

The IP address 172.8.9.28 is valid
The IP address 2001:0db8:85a3:0000:0000:8a2e:0370:7334 is valid

2. Using OWASP Validation Regex

We can also use OWASP Validation Regex, which is considered to be very safe. We can use the following regular expression to check for a valid IP Address.

^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$

 
ESAPI validation routine can also be used which provides the following regular expression.

Validator.IPAddress=^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$

Download  Run Code

Output:

The IP address 172.8.9.28 is valid

 
Please note that the above regular expression will fail for "172.01.1.2" as both decimal and octal numbers are considered valid.

3. Using Guava Library

Guava InetAddresses class provides static utility methods pertaining to InetAddress instances. One such method is isInetAddress(), which checks if the specified string is a valid IP string literal or not.

Download Code

Output:

The IP address 172.8.9.28 is valid
The IP address 2001:0db8:85a3:0000:0000:8a2e:0370:7334 is valid

4. Using Java 8

In Java 8, we can simplify things with the help of Stream, as shown below:

Download  Run Code

Output:

The IP address 172.8.7.28 isn’t valid

5. Simple Regex + Custom Validations

This solution is similar to the internal implementation of Apache Commons InetAddressValidator class discussed before.

Download  Run Code

Output:

The IP address 172.8.7.28 is valid

6. Using InetAddress.getByName() method

Finally, we can also use the Inet4Address.getByName() provided by JDK, which causes DNS services to be accessed and checks the validity of the supplied IP address. This might not work for all theoretical valid IP addresses, and this method will be relatively slower than all other alternatives.

Download  Run Code

Output:

The IP address 172.8.7.28 is valid

That’s all about validating an IP address in Java.

 
Also See:

Validate an IP address in C++