I have a EditText in android where IP Address is supposed to be entered. On click of a button I want to check if the text retrieved from EditText does :
- not have any spaces
- not have any letters
-
not empty
-
contain only numbers
- and contain only periods
"."
I have this if else condition to check if the user is allowed to go to the next activity but it still has some bugs. I don’t know how to allow ONLY periods
if(((ip.length() != 0) || ip.contains(" ") == false || ip.matches("[a-z]+") == false) && (ip.matches("[0-9]+") && ip.contains(".")))
{
next = false;
}
else
{
next = true;
}
You can use this regex to check the input
EDIT:
Do not use regex as things will break when IPv6 comes, use this instead
http://commons.apache.org/validator/apidocs/org/apache/commons/validator/routines/InetAddressValidator.html