Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache UrlValidator for all scheme

I am using org.apache.commons.validator.UrlValidator for validating URL. It return true for http://www.google.com but false for www.google.com

I want to allow www.google.com also. I applied ALLOW_ALL_SCHEMES as parameter but it is not working. How to customize UrlValidator so that it returns true for www.google.com and http://localhost:8282/GFEReporting also. Is there any way to customize this so it can take all schemes and no scheme? Please tell me.

or is there any other way to do this?

Thanks in advance...

like image 924
Abhendra Singh Avatar asked Aug 06 '26 12:08

Abhendra Singh


2 Answers

UrlValidator validator = new UrlValidator() {
    /** allow missing scheme. */
    @Override
    public boolean isValid(String value) {
        return super.isValid(value) || super.isValid("http://" + value);
    }
};

validator.validate("google.com");
like image 133
Sergey Avatar answered Aug 09 '26 02:08

Sergey


private boolean isValidUrl(String value) {
    return UrlValidator.getInstance().isValid(value)
            || UrlValidator.getInstance().isValid("http://" + value);
}

This is simplified @Sergey's answer (without extending the UrlValidator() class).

like image 34
djm.im Avatar answered Aug 09 '26 03:08

djm.im



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!