How to Validate a Url/Website Name in Edittext in Android

how to validate a URL / website name in EditText in Android?

Short answer

Use WEB_URL pattern in Patterns Class

 Patterns.WEB_URL.matcher(potentialUrl).matches()

It will return True if URL is valid and false if URL is invalid.

Long answer

As of Android API level 8 there is a WEB_URL pattern. Quoting the source, it "match[es] most part of RFC 3987". If you target a lower API level you could simply copy the pattern from the source and include it in your application. I assume you know how to use patterns and matchers, so I'm not going into more details here.

Also the class URLUtil provides some useful methods, e.g:

  • isHttpUrl()
  • isValidUrl()

The descriptions of the methods are not very elaborate, therefore you are probably best of looking at the source and figuring out which one fits your purpose best.

As for when to trigger the validation check, there are multiple possibilities: you could use the EditText callback functions

  • onFocusChanged(), or
  • onTextChanged()

or use a TextWatcher, which I think would be better.

DON'T USE URLUtil to validate the URL as below.

 URLUtil.isValidUrl(url)

because it gives strings like "http://" as valid URL which isn't true

Validate Specfic URL input in EditText for android

String[] schemes = {"http","https"}; //DEFAULT schemes = "http", "https", "ftp"

UrlValidator urlValidator = new UrlValidator(schemes);

if (urlValidator.isValid("http://www.google.com")) {
//url is valid
}else{
//url is invalid
}

Use Apache commons-validator URLValidator class

Android - Button disabled until a valid URL is entered in edittext

  1. Url validation: Regular expression to match URLs in Java
  2. How to enable button just when user enters a valid URL:

Let's guess your EditText has the txt_url id:

EditText editText = (EditText) findViewById(R.id.txt_url);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}

@Override
public void afterTextChanged(Editable editable) {
// lets supouse validation method is called validUrl()
button.setEnabled(validUrl(editable.toString()));
}
});

Validation in EditText allow IP or web Url host

ip

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])\\." +
"([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();
}

url

try {
new java.net.URI(url);
} catch(MalformedURLException e) {
// url badly formed
}

How do I validate a URL in a TextWatcher while also matching with an empty string

String pattern = "(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9]\.[^\s]{2,})";

Will match the following cases

  • http://www.foufos.gr
  • https://www.foufos.gr
  • http://foufos.gr
  • http://www.foufos.gr/kino
  • http://www.t.co
  • http://t.co
  • http://werer.gr
  • www.foufos.gr
  • www.mp3.com
  • www.t.co

Will NOT match the following

  • www.foufos
  • http://www.foufos
  • http://foufos
  • www.mp3#.com
  • www.foufos-.gr
  • www.-foufos.gr

Concerning empty string, first check if it is empty and then check for pattern:

if(yourstring.length() == 0 ||  yourstring.matches(pattern)) {
// do something
}else{
// show validation warning
}

source

How to know if an entered edittext text is a link or not?

You can use URLUtil.isValidUrl(url) to check if a string is a valid URL.

P.S - Do note that there are many ways that a URL can be well-formed but not retrievable. It's always best to ensure that you're catching any potential exceptions that might be thrown even after you check if the URL is valid.

How to validate an entered URL in android studio and if its not valid show the alert in pop up

Try this

Patterns.WEB_URL.matcher(potentialUrl).matches()

It will return True if URL is valid and false if URL is invalid.

And simple show alert dialog

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);

// set title
alertDialogBuilder.setTitle("Your Title");

// set dialog message
alertDialogBuilder
.setMessage("Click yes to exit!")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
MainActivity.this.finish();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});

// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();

// show it
alertDialog.show();
}
});


Related Topics



Leave a reply



Submit