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?
compile(regex) val m = p. matcher(ip) return m. matches() } val inputIP = "127.1. 1.775" println("Input: " + inputIP) println("Output: " + isValidIPAddress(inputIP)) ... ...
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.
Patterns.IP_ADDRESS.matcher(url).matches();
You can use the Patterns.IP_ADDRESS global regex.
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 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With