Validate Ipv4 Address in Java

Validate IPv4 address in Java

Please see https://stackoverflow.com/a/5668971/1586965 which uses an apache commons library InetAddressValidator

Or you can use this function -

public static boolean validate(final String ip) {
String PATTERN = "^((0|1\\d?\\d?|2[0-4]?\\d?|25[0-5]?|[3-9]\\d?)\\.){3}(0|1\\d?\\d?|2[0-4]?\\d?|25[0-5]?|[3-9]\\d?)$";

return ip.matches(PATTERN);
}

Validating IPv4 string in Java

Here is an easier-to-read, slightly less efficient, way you could go about it.

public static boolean validIP (String ip) {
try {
if ( ip == null || ip.isEmpty() ) {
return false;
}

String[] parts = ip.split( "\\." );
if ( parts.length != 4 ) {
return false;
}

for ( String s : parts ) {
int i = Integer.parseInt( s );
if ( (i < 0) || (i > 255) ) {
return false;
}
}
if ( ip.endsWith(".") ) {
return false;
}

return true;
} catch (NumberFormatException nfe) {
return false;
}
}

Check valid IPv4 Address in Java

There's a reason you're getting a "valid" result: 200 is a valid IPv4 address.

See, to the computer, an IPv4 address is just a 32-bit number. The dots are entirely for our convenience, because we humans suck at memorizing big precise numbers. But they don't have to be there; there are rules about how an address gets parsed depending on how many parts it has.

When an address consists of one number, it's considered a 32-bit number, and each byte is 8 bits of that number. If you were to parse "200" as an IP address, it would be equivalent to 0.0.0.200. Likewise, "2130706433" would be equivalent to 127.0.0.1.

There are also standards for when an address has two parts like 0.200 (first part is the first byte, and the second part is a 24-bit number representing the other 3 bytes), and even 0.0.200 (first two numbers are bytes, the last part is 16 bits and takes up the other 2 bytes). The "unusual" formats are leftovers from the days of IP address classes, but almost all software that has to parse addresses will understand them. (If you pop open your browser and go to http://1249739112* or even http://74.125.33128*, for example, Google's home page will come up.)

* See the comments for clickable links. Thanks, "link validator". :P

See http://download.oracle.com/javase/6/docs/api/java/net/Inet4Address.html or http://www.perlmonks.org/?node_id=221512, or http://en.wikipedia.org/wiki/IPv4#Address_representations, for some more details.

Java understands these formats as well (as does .net, as well as any decent OS), and parses the address correctly whether it contains 1, 2, 3, or 4 parts.

If you want to check that a would-be address actually looks like "xxx.xxx.xxx.xxx", then you'll probably want to explicitly check that using a pattern, or using a validation library that considers 32-bit numbers as invalid addresses (even though they are valid). I wouldn't bother, though -- if you use the lookup functions provided, you can accept an address in any standard format and it will work.

(All this mess changes with IPv6; there's a much stricter format, and you can't just type in some 36-digit number and expect it to work. But the platform still knows how to parse an address, and you should trust it to do so.)

How to verify if a IP address is valid on Java?

In your specific scenario, you can add a filter and check the resulting arrays length:

String ipAddress = input.nextLine();
boolean ipValid = Arrays
.stream(ipAddress.split("\\."))
.mapToInt(Integer::parseInt)
.filter(x -> x >= 0 && x <= 255) // remove invalid numbers
.toArray().length == 4; // if the resulting length is 4, the ip is valid

Note: your approach will fail if Integer.parseInt throws an exception (i.e. if the user enters some letters), and will consider an IP valid even if there is some extra dot (like 192.168.0.1.)

Validating IPv4 addresses with regexp

You've already got a working answer but just in case you are curious what was wrong with your original approach, the answer is that you need parentheses around your alternation otherwise the (\.|$) is only required if the number is less than 200.

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

Validate IPv4 address in Java

Please see https://stackoverflow.com/a/5668971/1586965 which uses an apache commons library InetAddressValidator

Or you can use this function -

public static boolean validate(final String ip) {
String PATTERN = "^((0|1\\d?\\d?|2[0-4]?\\d?|25[0-5]?|[3-9]\\d?)\\.){3}(0|1\\d?\\d?|2[0-4]?\\d?|25[0-5]?|[3-9]\\d?)$";

return ip.matches(PATTERN);
}

Validate Ipv6/ipv4 address using InetAddress

One possible approach is to use Guava's InetAddresses.forString() parser:

https://code.google.com/p/guava-libraries/source/browse/guava/src/com/google/common/net/InetAddresses.java



Related Topics



Leave a reply



Submit