输入文本的字符限制 [英] Character limit on input text
本文介绍了输入文本的字符限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想让我的文本框只允许数字并且有字符限制.
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.
这是我所拥有的...
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"
这篇关于输入文本的字符限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文