I want to validate an IPv4 address using Java. It should be written using the dot-decimal notation, so it should have 3 dots (".
"), no characters, numbers in between the dots, and numbers should be in a valid range. How should it be done?
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);
}