Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GWT java URL Validator

Does someone knows a function that validate if a url is valid or not purely in GWT java without using any JSNI

like image 519
Noor Avatar asked Dec 28 '22 03:12

Noor


1 Answers

I am using this one (making use of regular expressions):

private RegExp urlValidator;
private RegExp urlPlusTldValidator;
public boolean isValidUrl(String url, boolean topLevelDomainRequired) {
    if (urlValidator == null || urlPlusTldValidator == null) {
        urlValidator = RegExp.compile("^((ftp|http|https)://[\\w@.\\-\\_]+(:\\d{1,5})?(/[\\w#!:.?+=&%@!\\_\\-/]+)*){1}$");
        urlPlusTldValidator = RegExp.compile("^((ftp|http|https)://[\\w@.\\-\\_]+\\.[a-zA-Z]{2,}(:\\d{1,5})?(/[\\w#!:.?+=&%@!\\_\\-/]+)*){1}$");
    }
    return (topLevelDomainRequired ? urlPlusTldValidator : urlValidator).exec(url) != null;
}
like image 73
svub Avatar answered Jan 05 '23 10:01

svub