Skip to content

Instantly share code, notes, and snippets.

@kakuty
Created July 16, 2017 10:10
Show Gist options
  • Save kakuty/a8f81f0440c427daf5abfafdc1cfa2f9 to your computer and use it in GitHub Desktop.
Save kakuty/a8f81f0440c427daf5abfafdc1cfa2f9 to your computer and use it in GitHub Desktop.
Check if URL is valid
function isURL(str) {
var urlRegex = '^(?!mailto:)(?:(?:http|https|ftp)://)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$';
var url = new RegExp(urlRegex, 'i');
return str.length < 2083 && url.test(str);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment