Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you validate an IP address on Android?

Tags:

android

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?

like image 554
Sri Sri Avatar asked Sep 13 '10 05:09

Sri Sri


People also ask

How do you check IP address is valid or not in Android?

compile(regex) val m = p. matcher(ip) return m. matches() } val inputIP = "127.1. 1.775" println("Input: " + inputIP) println("Output: " + isValidIPAddress(inputIP)) ... ...

How do I find IP address on Android?

To check IP address of the local network on the Android device: Go to Settings → Network & internet on the tablet and select Wi-Fi. Tap the name of active network and expand the Advanced section. Find the Network details field with the local IP address.


2 Answers

Patterns.IP_ADDRESS.matcher(url).matches(); 
like image 144
Artyom Avatar answered Oct 02 '22 08:10

Artyom


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 } 
like image 41
Rorist Avatar answered Oct 02 '22 08:10

Rorist