Created
May 20, 2016 09:29
-
-
Save AdonaiAraya/d5f04a6b335d192caf0c0bdc6dbe15b6 to your computer and use it in GitHub Desktop.
Angular directive for limit the max length in input number
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
/* | |
<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