35

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?

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
Sri Sri
  • 3,107
  • 7
  • 34
  • 37

5 Answers5

49
Patterns.IP_ADDRESS.matcher(url).matches();
Artyom
  • 1,099
  • 15
  • 18
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
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