输入文字的字数限制
问题描述:
我想在我的文本框中只允许数字,并且对它也有字符限制.
I want to my textbox to only allow numbers and also have a character limit on it.
目前,我正在使用数字...现在我在解决如何限制字符方面遇到问题.
Currently, I have the numbers working... Now I am having issues figuring out how to limit the characters.
这就是我所拥有的...
Here is what I have...
JS:
app.directive('numbersonly', function () {
return {
restrict: 'A',
link: function (scope, elm, attrs, ctrl) {
elm.on('keydown', function (event) {
if (event.which == 64 || event.which == 16 && elm.val().length > 4) {
return false;
} else if (event.which >= 48 && event.which <= 57) {
return true;
} else if (event.which >= 96 && event.which <= 105) {
return true;
} else if ([8, 13, 27, 37, 38, 39, 40].indexOf(event.which) > -1 && elm.val().length > 4) {
return true;
} else {
event.preventDefault();
return false;
}
});
}
}
});
标记:
<input type="text" id="txtEmpAge" data-ng-model="newemployee.Age" class="form-control" numbersonly required />
答
以html输入属性的长度.我们可以以多种形式使用它:
In html input attribute length. we can use it in multiple forms:
长度,最小长度和最大长度
Length, Minlength and maxlength
尝试一下
maxlength="5"