Skip to content

Instantly share code, notes, and snippets.

@thewinterwind
Created October 17, 2013 07:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thewinterwind/7020392 to your computer and use it in GitHub Desktop.
Save thewinterwind/7020392 to your computer and use it in GitHub Desktop.
Validating a URL with Javascript
// I didn't write this function, only modified it slightly. It works well though!
function valid_url(str) {
var pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name
'((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path
'(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string
'(\\#[-a-z\\d_]*)?$','i'); // fragment locator
return pattern.test(str) ? true : false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment