[英]How to allow all characters but number from input
At the moment, I have an input.目前,我有一个输入。 I am allowed to enter any characters, even special characters, no digits.我可以输入任何字符,甚至是特殊字符,不能输入数字。
What I've tried so far is to setup a keydown and a keyup event.到目前为止,我尝试的是设置 keydown 和 keyup 事件。
ng-keydown="vm.preventNumberInput($event)"
ng-onkeyup="vm.preventNumberInput($event)"
vm.preventNumberInput = function (e) {
var keyCode = (e.keyCode ? e.keyCode : e.which);
if (keyCode > 47 && keyCode < 58 || keyCode > 95 && keyCode < 107) {
e.preventDefault();
}
}
This works okay, but it prevents me from adding special characters like.@#%^&*.这工作正常,但它阻止我添加特殊字符,如.@#%^&*。
May I ask how do I allow characters from being entered into my input that aren't digits.请问如何允许在我的输入中输入不是数字的字符。
Check the event's key
property to get the pressed key.检查事件的key
属性以获取按下的键。 If it matches \d
(a digit), call preventDefault
:如果它匹配\d
(一个数字),调用preventDefault
:
vm.preventNumberInput = function (e) {
if (/\d/.test(e.key)) {
e.preventDefault();
}
}
Any characters other than digits will be allowed.允许使用除数字以外的任何字符。
(note that the keyCode
and which
properties are deprecated, and should be avoided when possible) (请注意,不推荐使用keyCode
和which
属性,应尽可能避免使用)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.