Azhar Shaikh Azhar Shaikh - 7 months ago 57
Android Question

Validation in EditText allow IP or web Url host

I am in need to give validation on my EditText such that it allows me to enter a valid

ip address format ( ?.?.?.? ) ie example


web url format ( www.?.? ) ie example

logic is that if user type any numeric value first then validation(IP) will do action

else user must write "www" before any web String

Note: it must perform onKeyListener() of EditText, i mean while user giving input

In short - I am not going to check when user completes input and press OK button

Any idea appreciated,



private static final String PATTERN = 
        "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
        "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
        "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +

public static boolean validate(final String ip){          
      Pattern pattern = Pattern.compile(PATTERN);
      Matcher matcher = pattern.matcher(ip);
      return matcher.matches();             


try {
} catch(MalformedURLException e) {
    // url badly formed