[Solved] i want to make typing ‘http://’ is not necessary. how can i do?


I think this can be achieved by checking whether the string is a valid url using a regex.

This regex is got from another SO post:

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

You first check whether url is a valid url using this regex. If the url is something like google.com, it does not match. If the url does not match, add “https://” to the start of it and see if it matches. If it matches this time, load the url, otherwise, tell the user that it’s not a valid url or something of the sort.

Pattern p = Pattern.compile("\\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]");
if (!p.matcher(url).matches() &&
        (p.matcher("https://" + url).matches()) {
    url = "https://" + url;
    // load the url
}

4

solved i want to make typing ‘http://’ is not necessary. how can i do?