URL Validation in JavaScript

This won’t catch every malformed URL, but it’s a pretty good filter:

function test_url(url) {
  // comments:  (protocol )(user&pass   ) (subdomains&domain) (TLD            )(port   ) (path & query string   )
  var regex = /^https?:\/\/(\w+(:\w+)?\@)?([\w\-]+|\.[\w\-]+)+(\.[a-zA-Z]{2,6})(:[0-9]+)?(\/[\w#!:.?+=&%@!\-\/]*)?$/i;
  return url.match(regex);
}

You can follow any responses to this entry through the RSS 2.0 feed. Responses are currently closed, but you can trackback from your own site.

1 Comment »