153
rated 0 times
[
158]
[
5]
/ answers: 1 / hits: 6514
/ 11 Years ago, mon, february 10, 2014, 12:00:00
Using angularjs, I'm not satisfied with the ng-maxlength
tag. I want the input field to block the user from entering additional characters when the max length has been hit. So instead of ng-maxlength
, I tried writing a directive that would validate the input on keydown/keypress:
.directive('vldMaxLength', function() {
return function (scope, element, attr) {
element.bind('keydown keypress', function (event) {
if(event.target.value.length >= 10 && (event.which !== 8 && event.which !== 46)) {
event.preventDefault();
}
});
};
})
This works decently, but the user is unable to highlight a portion of the string and replace with different characters. Maybe there's something I can add to the directive to allow for this? Or maybe there's an easier way to get this functionality instead of using a directive.
JSFiddle: http://jsfiddle.net/DwKZh/147/
More From » forms