3

I need to validate if string entered in TextEdit is a web address eg. "www.stackoverflow.com" or an ip address eg. "64.34.119.12". I have tried this two methods without success. I have private class variable named ip.

Method 1:

public boolean isAdress(){

        boolean isaddr = true;
        try
        {
            ip = new NetTask().execute(""+textEdit1.getText()).get();
        }
        catch (Exception ex)
        {
            isaddr = false;
        }
        return isaddr;
    }

Method 2 is the one were I check string before sending it to NetTask.

public boolean isAdress(){
        String adress = textEdit1.getText().toString();
        boolean isaddr = true;
        if (adress.length() > 0) {
            String[] nums = adress.split(".");
            if (nums.length == 4) {
                for (String str : nums) {
                    int i = Integer.parseInt(str);
                    if ((i < 0) || (i > 255)) {
                        isaddr = false;
                    }
                }
            } 
        }
        return isaddr;
    }

this second method also doesn't wotk, but even if it did, it wouldn't be able to validate web adress.

So it there any way I can validate string for both of this cases?

EDIT: After reading about regex I tried this method also:

private String regex = "\\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";

public boolean isAdress(){
        String adress = textEdit1.getText().toString();
        try {
            Pattern patt = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
            Matcher matcher = patt.matcher(adress);
            return matcher.matches();
        } catch (RuntimeException e) {
        return false;
    }           
    }

but it seems to return false all the time.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Rohit Malish
  • 3,209
  • 12
  • 49
  • 67
  • Anything can be a web address. Are you sure you need this? – bzlm Jul 30 '12 at 14:09
  • Check out this answer: http://stackoverflow.com/a/163398/1177083 – 4ndro1d Jul 30 '12 at 14:11
  • Yes I do, I need to be able to tell if it is adress or not, because if I pass that string to my other method my program will crash. It is must that string is ip or web adress – Rohit Malish Jul 30 '12 at 14:11

4 Answers4

5

Short answer: Try using regex!

EDIT:

if(textEdit1.getText().matches(REGEX_URL)) {
    //DO URL THINGS
}

if(textEdit1.getText().matches(REGEX_IPADDRES)) {
    //DO IP THINGS
}

If you google you can find the correct REGEX strings for IP addresses and urls...

NOTE: A regex for urls can be different for what you want, do you only want http:// https:// or all valid urls (like market://)...

Ferdau
  • 1,524
  • 1
  • 12
  • 21
  • Now you've lured @RohitMalish into the [dark pit of URL validation](http://mathiasbynens.be/demo/url-regex). – bzlm Jul 30 '12 at 14:10
  • How easy it is to use regex? Im pretty new to android. That was easy to do on desktop when you didnt need NetTask stuff. – Rohit Malish Jul 30 '12 at 14:13
  • I need regex for strings without any kind of http:// etc. Adress must be in this form to get accepted "www.google.com, www.abc.com, www.stackoverflow.com, www.something.eu, etc" – Rohit Malish Jul 30 '12 at 14:33
  • Well, like I said: use regex, so google how to make a regex or google one that fits you... – Ferdau Jul 30 '12 at 14:34
  • ok, i already tried regex, but it seems to return only false. I edited my post and added regex method – Rohit Malish Jul 30 '12 at 14:37
4

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

Balwinder SIngh
  • 1,831
  • 2
  • 24
  • 28
0

Validation of IP Address in Kotlin which returns true or false

fun isValidIPAddress(ip:String):Boolean {
   val reg0To255 = ("(\\d{1,2}|(0|1)\\" + "d{2}|2[0-4]\\d|25[0-5])").toRegex()
   return reg0To255.matches(ip)
}

val inputIP = "127.1.1.775"
println("Input: " + inputIP)
println("Output: " + isValidIPAddress(inputIP))

Input: 127.1.1.775 Output: false

Fakhar
  • 3,946
  • 39
  • 35
-3

how about simpler approach? detect if it is IP address, e.g.

public static boolean isIP(String input) {

        if (input.contains(".") && input.length()>1) {
            return TextUtils.isDigitsOnly( input.replace(".", "").trim() );
        }
        else {
            return false;
        }
    }
Yilmaz Guleryuz
  • 9,313
  • 3
  • 32
  • 43