Created
April 2, 2018 08:00
-
-
Save alegut/66749e098f189d7b11a931e1d2aa23b5 to your computer and use it in GitHub Desktop.
JS validate URL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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