Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 输入类型编号maxlength 5_Javascript - Fatal编程技术网

Javascript 输入类型编号maxlength 5

Javascript 输入类型编号maxlength 5,javascript,Javascript,我想将输入类型数字限制为最多5个数字,我使用下面的代码,这很好,唯一的问题是对于backspace,我必须使用我不想使用的event.keycode。除了使用退格键码,还有其他选择吗 var input = document.getElementById('input'); input.addEventListener('keypress',showData,false) function showData(event) { if(event.target.value.length<

我想将输入类型数字限制为最多5个数字,我使用下面的代码,这很好,唯一的问题是对于backspace,我必须使用我不想使用的event.keycode。除了使用退格键码,还有其他选择吗

var input = document.getElementById('input');
input.addEventListener('keypress',showData,false)
function showData(event)
{
   if(event.target.value.length<=5)
   {
      return true;
   }
   else
   {
      event.preventDefault();
   }

}
var input=document.getElementById('input');
input.addEventListener('keypress',showData,false)
函数showData(事件)
{

如果(event.target.value.length,为什么不使用:

<input type="number" max="99999">


这仍然不会阻止用户手动输入大于99999的值,但输入元素将无效。

为什么不使用:

<input type="number" max="99999">
<input type="number" max="99999" />

这仍然不会阻止用户手动输入大于99999的值,但输入元素将无效。


<input type="number" max="99999" />



如果需要,那么如果用户尝试键入5个以上的数字,则只保留5个数字:

input.oninput = function() {
    if (this.value.length > 5) {
        this.value = this.value.slice(0,5); 
    }
}

如果需要,如果用户尝试键入5个以上的数字,则只保留5个数字:

input.oninput = function() {
    if (this.value.length > 5) {
        this.value = this.value.slice(0,5); 
    }
}




对于输入类型“number”无效正确。应删除注释:p为什么不使用
keyup
而不是
keypress
?那么您就不必担心退格问题。此答案描述了如何将输入类型文本限制为仅允许数字字符。您可以扩展此选项,然后使用maxlength=5。@mikeyq6:尝试的keyup不适用于输入类型无效“数字"正确。应该删除注释:p为什么不使用
keyup
而不是
keypress
?那么你就不必担心退格问题。这个答案描述了如何将输入类型文本限制为只允许数字字符。你可以扩展它,然后使用maxlength=5。@mikeyq6:尝试过的keyup不起作用是的,我检查过了,但需要注意,输入不超过5位应该是可见的。是的,我检查了,但要求是输入不超过5位应该是可见的。我搜索了stackoverflow,但我只找到了最小值和最大值解决方案,不幸的是,这不是我需要的。删除最小值属性。或者用户jquery验证程序pluginI ha我搜索了stackoverflow,但是我只找到了最小值和最大值的解决方案,不幸的是,这不是我所需要的。删除最小值属性。或者用户jquery validator pluginThankshelped@user3045179您可以在oninput函数中将其简化为:
this.value=(this.value.length>5)?this.value.slice(0,5):this.value;
谢谢helped@user3045179您可以在oninput函数中将其简化为:
this.value=(this.value.length>5)?this.value.slice(0,5):this.value;