Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/81.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中,我们如何在文本框中只允许数字?_Javascript_Html - Fatal编程技术网

在JavaScript中,我们如何在文本框中只允许数字?

在JavaScript中,我们如何在文本框中只允许数字?,javascript,html,Javascript,Html,我需要每个按键的确认 <strike><input type="number" value="" id="phno" name="num" onkeydown="return isNumber()" /></strike> function isNumber(){ var s= document.getElementById("phno").value; if(isNaN(phno)){ alert("mistmatch");

我需要每个按键的确认

<strike><input type="number"  value="" id="phno" name="num" onkeydown="return isNumber()" /></strike>

function isNumber(){
   var s= document.getElementById("phno").value;
   if(isNaN(phno)){
     alert("mistmatch");
   }
}

函数isNumber(){
var s=document.getElementById(“phno”).value;
如果(isNaN(phno)){
警报(“误匹配”);
}
}
如何在每次按键时进行验证(可能是keyup事件)

使用此DOM

<input type='text' onkeypress='validate(event)' />
希望对你有所帮助你可以试试这个

<input type="text" id="myTextBox" />
这将允许用户输入十进制值或整数。

尝试以下操作:

纯JS

剧本

html

html



您没有提出问题。与JSP无关。这是一个HTML/Javascript问题。可能与
<input type="text" id="myTextBox" />
$("#myTextBox").on('keydown', function(e) {
  var key = e.keyCode ? e.keyCode : e.charCode;
  var value = $(this).val();
  if (key > 57 && ((key == 190 && value.indexOf('.') >= 0) || key != 190)) {
    e.preventDefault();
  }
});
var digits = function(box) {
    box.value = box.value.replace(/[^0-9]/g, '');
};
<input type="text" placeholder="Digits only" onkeyup="digits(this)"/>
$(function(){
    $('.digits').on('input', function(e){
        this.value = this.value.replace(/[^0-9]/g, '');
    });
});
<input type="text" placeholder="Digits only" class="digits"/>