Rohit Malish
Rohit Malish

Reputation: 3229

Checking if string is web address or ip on android

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.

Upvotes: 2

Views: 4880

Answers (4)

Fakhar Iqbal
Fakhar Iqbal

Reputation: 4069

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

Upvotes: 0

Balwinder SIngh
Balwinder SIngh

Reputation: 1961

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

Upvotes: 4

Yilmaz Guleryuz
Yilmaz Guleryuz

Reputation: 9745

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

Upvotes: -2

Ferdau
Ferdau

Reputation: 1558

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://)...

Upvotes: 6

Related Questions