Last active
September 28, 2016 17:23
-
-
Save goors/76308e8ad8c676c601f71aa48d6c8724 to your computer and use it in GitHub Desktop.
Validate website (url) angular directive. localhost is "invalid"
This file contains 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
.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