Skip to content Skip to sidebar Skip to footer

Javascript Regex For Url Without Http

How do I modify the regex below so that a url without 'http://', 'https://', 'ftp://', or 'mailto://' still comes back as valid? The REGEX is taken from validator.js var url = 'ww

Solution 1:

Weird requirement for validating URL.

But you can do it by making (?:(?:http|https|ftp)://) optional using ? like this (?:(?:http|https|ftp)://)?


Solution 2:

If you're using validation.js, it seems to support an option to disable protocol matching:

isURL(str [, options]) - check if the string is an URL. options is an object which defaults to { protocols: ['http','https','ftp'], require_tld: true, require_protocol: false, require_host: true, require_valid_protocol: true, allow_underscores: false, host_whitelist: false, host_blacklist: false, allow_trailing_dot: false, allow_protocol_relative_urls: false }.

So you could just do :

var isUrl = validator.isUrl(url, {require_protocol:false, require_valid_protocol:false});

It looks like that is the default though, so maybe you're not using the library directly.


Post a Comment for "Javascript Regex For Url Without Http"