Skip to content

Instantly share code, notes, and snippets.

@goors
Last active September 28, 2016 17:23
Show Gist options
  • Save goors/76308e8ad8c676c601f71aa48d6c8724 to your computer and use it in GitHub Desktop.
Save goors/76308e8ad8c676c601f71aa48d6c8724 to your computer and use it in GitHub Desktop.
Validate website (url) angular directive. localhost is "invalid"
.directive('validateWebsite', function() {
return {
link: function(scope, elm) {
function isUrlValid(userInput) {
var regexQuery = "^(https?://)?(www\\.)?([-a-z0-9]{1,63}\\.)*?[a-z0-9][-a-z0-9]{0,61}[a-z0-9]\\.[a-z]{2,6}(/[-\\w@\\+\\.~#\\?&/=%]*)?$";
var url = new RegExp(regexQuery,"i");
if (url.test(userInput)) {
return true;
}
return false;
}
elm.on("keyup",function(){
if( isUrlValid(elm.val())){
elm.parent().addClass('valid');
}else{
elm.parent().removeClass('valid');
}
});
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment