Skip to content

Instantly share code, notes, and snippets.

@alegut
Created April 2, 2018 08:00
Show Gist options
  • Save alegut/66749e098f189d7b11a931e1d2aa23b5 to your computer and use it in GitHub Desktop.
Save alegut/66749e098f189d7b11a931e1d2aa23b5 to your computer and use it in GitHub Desktop.
JS validate URL
function validateUrl(url) {
var re = /^(ftp|http|https):\/\/[^ "]+$/.test(url);
return re;
}
function validate(data) {
var url = data.val();
var $result = data.next();
$result.text("");
if (validateUrl(url)) {
$result.text(url + " is valid :)");
$result.css("color", "green");
return true;
} else {
$result.text(url + " is not a link :(");
$result.css("color", "red");
return false;
}
}
var validState = true;
$(document).on('click','.item_link',function (e){
e.preventDefault();
var url = $(this);
if(!validate(url)){
validState = false;
return false;
}else{
validState = true;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment