I am using simple socket communication between Android (as the client) and PC (as the server). I am having the user input the IP address into an EditText
field and I want to validate the IP address. How do you validate an IP address on Android?
Asked
Active
Viewed 2.6k times
35

Josh Correia
- 3,807
- 3
- 33
- 50

Sri Sri
- 3,107
- 7
- 34
- 37
5 Answers
49
Patterns.IP_ADDRESS.matcher(url).matches();

Artyom
- 1,099
- 15
- 18
-
As simple as it can be. – Ahsan Oct 26 '17 at 12:29
-
Perrfect...Simple and Classy – Shachi Apr 16 '18 at 13:44
-
1how todo ip with port ? – Michael Mao Aug 10 '18 at 04:39
-
1@MichaelMao To validate a port you just verify it is a number between 0-65535 – Always Lucky Sep 29 '18 at 15:28
-
Best answer ever. – Sam Chen Jan 10 '20 at 16:22
45
API Level 8+:
You can use the Patterns.IP_ADDRESS global regex.
API Level 1-7:
You may directly include this regex in your project if you target devices with android < 2.2:
private static final Pattern IP_ADDRESS
= Pattern.compile(
"((25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9])\\.(25[0-5]|2[0-4]"
+ "[0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]"
+ "[0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}"
+ "|[1-9][0-9]|[0-9]))");
Matcher matcher = IP_ADDRESS.matcher("127.0.0.1");
if (matcher.matches()) {
// ip is correct
}

Josh Correia
- 3,807
- 3
- 33
- 50

Rorist
- 810
- 8
- 9
19
To check the IP as it's being entered you might want to use this instead:
private static final Pattern PARTIAl_IP_ADDRESS =
Pattern.compile("^((25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[0-9])\\.){0,3}"+
"((25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[0-9])){0,1}$");
ipEditText.addTextChangedListener(new TextWatcher() {
@Override public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override public void beforeTextChanged(CharSequence s,int start,int count,int after) {}
private String mPreviousText = "";
@Override
public void afterTextChanged(Editable s) {
if(PARTIAl_IP_ADDRESS.matcher(s).matches()) {
mPreviousText = s.toString();
} else {
s.replace(0, s.length(), mPreviousText);
}
}
});

Graeme
- 25,714
- 24
- 124
- 186
-
-
This is awesome and pretty easy to generalise into some kind of "RegexValidator" that can be easily added to any EditText for other use cases :) – user1405990 Oct 19 '16 at 02:20
-
Update: added a (Kotlin) gist here: https://gist.github.com/kiwiandroiddev/5c3b8d527aaf8b8318007fb685f23c5d – user1405990 Oct 19 '16 at 02:56
6
Reading description about Patterns.IP_ADDRESS I've seen that it's will be deprecated (on API 31) and needs use another function (InetAddresses.isNumericAddress) for check ip adress.
The result:
fun isIpValid(ip: String): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
InetAddresses.isNumericAddress(ip)
} else {
Patterns.IP_ADDRESS.matcher(ip).matches()
}
}

SerjantArbuz
- 982
- 1
- 12
- 16
0
The solution in Kotlin to find a Valid IP:
import java.util.regex.*;
...
...
fun isValidIPAddress(ip:String):Boolean {
// Regex for digit from 0 to 255
val reg0To255 = ("(\\d{1,2}|(0|1)\\" + "d{2}|2[0-4]\\d|25[0-5])")
// regex 0 To 255 followed by a dot, 4 times repeat
// validation an IP address.
val regex = (reg0To255 + "\\."
+ reg0To255 + "\\."
+ reg0To255 + "\\."
+ reg0To255)
val p = Pattern.compile(regex)
val m = p.matcher(ip)
return m.matches()
}
val inputIP = "127.1.1.775"
println("Input: " + inputIP)
println("Output: " + isValidIPAddress(inputIP))
...
...
Input: 127.1.1.055 Output: true
Input: 127.ip.1.75 Output: false
Input: 127.201.1.775 Output: false

Fakhar
- 3,946
- 39
- 35