validationInput(event) {
if (event.keyCode == 13 && this.pageInput) {
if (event.keyCode >= 48 && event.keyCode <= 57) {
var inputNumber = parseInt((this.pageInput == undefined ? "" : this.pageInput) + event.key
);
return inputNumber !== 0
? inputNumber <= this.totalPages
? true
: false
: false;
}
return false;
}
这是我的函数,当用户键入时验证pageInput 例如:总页数10,用户输入的内容必须少于总页数!因此,用户只能键入小于或等于10的数字! 我有一个错误,当输入已经有如下图所示的数字时,我无法键入任何数字! 因为它已经有数字“ 2”,所以突出显示数字“ 2”时我不能输入任何数字!仅当我清除数字“ 2”时,才输入新的
答案 0 :(得分:0)
Try below code. This will work for numpad keys and other numeric keys on the keyboard. I have not added the exclusion list. you can add and else condition and have all the keys that you don't want to be pressed. One more thing, in your code if you press backspace or try to delete the number, then false will be returned and nothing will happen. Also, use this code with keydown event.
validationInput(event) {
if ((event.keyCode >= 48 && event.keyCode <= 57)||(event.keyCode >= 96 && event.keyCode <= 105)) {
var inputNumber = parseInt((this.pageInput == undefined ? "" : this.pageInput) + event.key);
var writeNum = inputNumber !== 0
? inputNumber <= this.totalPages
? true
: false
: false;
return writeNum;
}
}