GWT java URL Validator

Does anyone know a function that validates a URL or not just in GWT-java without using JSNI

+3
source share
3 answers

I use this (using 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;
}
+10
source

org.apache.commons.validator.UrlValidator and the static isValid method (String url) can help here.

0
source
0

Source: https://habr.com/ru/post/1781415/


All Articles