开发者

How do you validate an IP address on Android?

开发者 https://www.devze.com 2023-01-16 02:19 出处:网络
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. H

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?


Patterns.IP_ADDRESS.matcher(url).matches();


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
}


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);
        }
    }
});


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()
    }
}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号