Validate a WEB URL in Android
URLUtil.isHttpsUrl(String url), URLUtil.isHttpUrl(String url) and URLUtil.isValidUrl(String url) returns true in most of the cases.After reading few post on stackoverflow i got this very easy method for validation.This solution uses regular expressions.Google Android SDK provides an interesting class Patterns(Added in API level 8). Which we can use to validate our WEB URL address.
Bellow is the example to validate Web-URL
EditText textField = (EditText) findViewById(R.id.webaddressURL);
String enteredUrl = textField.getText().toString();
if (Patterns.WEB_URL.matcher(enteredUrl).matches()) {
Toast.makeText(this, "URL is valid!", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(this, "URL is invalid!", Toast.LENGTH_LONG).show();
}
No comments:
Post a Comment