Validate an IP address in Java

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


 

An IP address in IPv4 is defined as a 32-bit number. An IPv4 address is 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 will validate IPv4 addresses by checking that given addresses consists of exactly four “decimal” numbers ranging from 0 and 255, separated by dots. i.e. valid IP address format is "xxx.xxx.xxx.xxx" where xxx lies between 0 to 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 below 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 a 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 below 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.

 

Download

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 are considered to be very safe. Below regular expression can be used 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 above regular expression will fail for "172.01.1.2" as both decimal and octal numbers are considered valid.

 

3. Using Guava

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   Run 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. Java 8

In java 8, we can simplify things with the help of Stream API 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 internal implementation of Apache Commons InetAddressValidator class discussed before.

 

Download   Run Code

Output:

The IP address 172.8.7.28 is valid

 

6. InetAddress.getByName()

Finally, we can also use 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 address and this method will be relatively slow than all other alternatives.

 

Download   Run Code

Output:

The IP address 172.8.7.28 is valid

 
Also See: Validate an IP address in C++

 
Thanks for reading.

Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 





Leave a Reply

Notify of
avatar
wpDiscuz