Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.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 使用js禁用文本输入中的数字输入_Javascript_Html - Fatal编程技术网

Javascript 使用js禁用文本输入中的数字输入

Javascript 使用js禁用文本输入中的数字输入,javascript,html,Javascript,Html,我必须做的是不允许在表单中的文本输入中引入数字字符,但我必须使用javascript。以下是我的HTML代码: <form id="formu"> <input type="text" id="num" /> </form> 下面是我的JS代码: function numero(theEvent){ var evento=theEvent||window.event; switch(evento.type){

我必须做的是不允许在表单中的文本输入中引入数字字符,但我必须使用javascript。以下是我的HTML代码:

<form id="formu">
<input type="text" id="num" />
</form>

下面是我的JS代码:

function numero(theEvent){

    var evento=theEvent||window.event;

        switch(evento.type){
            case 'keypress':
                if(document.getElementById("num").evento.keyCode>=48&&document.getElementById("num").evento.keyCode<=57){
                    return false;
                }
        }
    }
功能编号(事件){
var evento=theEvent | | window.event;
开关(事件类型){
“按键”案例:

如果(document.getElementById(“num”).evento.keyCode>=48&&document.getElementById(“num”).evento.keyCode您可以使用新模式attrib HTML

<input type="text" id="num" pattern="[A-Za-z]" title="Only Alphabets">

函数validateKeyStrokes(事件){
var charCode=(event.which)?event.which:event.keyCode;
如果(字符码>31&(字符码<48 | |字符码>57)){
返回false;
}
返回true;
}

试试这个

试试使用jQuery和一个用于输入文本字段过滤的插件


您还可以使用HTML5新输入类型编号:

<input type="number" name="num" id="num">


但是,如果您想提供旧浏览器支持,请小心使用…

您在哪里调用了numero(theEvent)?为什么要使用这么大的库来验证击键?在某些情况下,这可能会有点过火。另一方面,jQuery使许多常见任务变得更容易,这可能是使用它的理由。我也考虑过keypress事件,但它不会处理粘贴。
<input type="number" name="num" id="num">