我的html文件中有以下输入:
<input name="password"
id="newPasswordConfirmation"
ng-model="newPasswordConfirmation"
type="number"
inputmode="numeric"
placeholder=""
required
minlength="8"
maxlength="8"
autocomplete="off"
ng-maxlength="8"
autocorrect="off"
autocapitalize="off"
style="-webkit-text-security: disc; text-security: disc;">
我想将输入限制为8种算法,但是当我在移动设备(android)上键入内容时,我可以继续输入8种算法。为什么这不起作用,您如何设置此限制?
最佳答案
如注释中所述,maxlength
不适用于数字输入。从MDN:
maxlength:如果type属性的值为文本,电子邮件,搜索,密码,
tel或url,此属性指定最大字符数
(用户可以输入UTF-16代码单位)。对于其他控制
类型,则将其忽略。
这是一个小指令,您可以代替使用:
angular.module('myApp').directive('maxLength', function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
var maxLength = attrs.maxLength || false;
if (maxLength) element.on('keydown keypress keyup paste propertychange', function(e) {
if (element[0].value.length >= maxLength - 1) {
element[0].value = element[0].value.slice(0, maxLength)
}
})
}
}
});
<input type="number" max-length="8">
关于javascript - ng-maxlength的最大长度似乎都无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52495667/