Skip to content

Instantly share code, notes, and snippets.

@AdonaiAraya
Created May 20, 2016 09:29
Show Gist options
  • Save AdonaiAraya/d5f04a6b335d192caf0c0bdc6dbe15b6 to your computer and use it in GitHub Desktop.
Save AdonaiAraya/d5f04a6b335d192caf0c0bdc6dbe15b6 to your computer and use it in GitHub Desktop.
Angular directive for limit the max length in input number
/*
<input type='number' digit-limit="4" />
*/
.directive("digitLimit", function(){
return {
restrict: "A",
link: function(scope, element, attrs){
var max = attrs.digitLimit;
element.bind("keydown", function(e){
var currentValue = element.val();
if(currentValue.length >= max && e.keyCode != 8 && e.keyCode != 9){
e.preventDefault();
}
});
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment