Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.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 - Fatal编程技术网

Javascript 忽略键关闭时的输入字符

Javascript 忽略键关闭时的输入字符,javascript,Javascript,我想忽略手机输入中的某些字符,这样数据库就只有数字了。我知道我可以在服务器端(使用PHP)轻松做到这一点,但我正试图更好地理解js事件。。我的问题是: 如果我有一个基本的输入: var phoneInput = document.getElementById("phoneInput"); 我可以使用“onkeydown”添加一个事件监听器,效果很好 phoneInput.onkeydown = function(e){ var c = String.fromCharCode(e.keyCo

我想忽略手机输入中的某些字符,这样数据库就只有数字了。我知道我可以在服务器端(使用PHP)轻松做到这一点,但我正试图更好地理解js事件。。我的问题是:

如果我有一个基本的输入:

var phoneInput = document.getElementById("phoneInput");
我可以使用“onkeydown”添加一个事件监听器,效果很好

phoneInput.onkeydown = function(e){
  var c = String.fromCharCode(e.keyCode);
  var patt = /\d/;
  if(!patt.test(c)) return false;
};
但是如果我尝试使用“addEventListener”做同样的事情,返回false似乎没有任何作用

phoneInput.addEventListener("keydown",function(e){
  var c = String.fromCharCode(e.keyCode);
  var patt = /\d/;
  if(!patt.test(c)) return false;
});

我只是不明白为什么。提前感谢您为主题提供的任何光线。

我强烈建议不要更改用户的输入,或者阻止他们在输入内容时输入内容。它令人困惑,并导致糟糕的用户体验

理想情况下,您应该保留服务器端验证,然后使用HTML5功能,例如:

<input type="number" /> Allows only numbers
<input type="text" pattern="[0-9. -]*" /> Allows numbers, spaces, periods and hyphens
<input type="text" required /> Specifies a required field

我在做一个类似的项目。我就是这样做的

// prevent users from typing alpha/ symbol characters on select fields
$("#modal-region").on("keydown", "#markdown, #sku", function(e) {

    var key = e.which;
    // when a keydown event occurs on the 0-9 keys the value 
    // of the "which" property is between 48 - 57 
    // therefore anything with a value greater than 57 is NOT a numeric key

    if ( key > 57) {
        e.preventDefault();

    } else if (key < 48) {

    // we don't want to disable left arrow (37), right arrow (39), delete (8) or tab (9)
    // otherwise the use cannot correct their entry or tab into the next field!

        if (key != 8 && key != 9 && key != 37 && key != 39 ) {
            e.preventDefault();
        }
    }

});
//禁止用户在选定字段中键入字母/符号字符
$(“#模态区域”)。关于(“键控”、“标记”、“sku”,函数(e){
var-key=e.which;
//当0-9键上发生keydown事件时,该值
//“哪个”属性介于48到57之间
//因此,任何大于57的值都不是数字键
如果(键>57){
e、 预防默认值();
}否则,如果(键<48){
//我们不想禁用左箭头(37)、右箭头(39)、删除(8)或制表符(9)
//否则,用户无法将其条目或选项卡更正到下一个字段中!
如果(键!=8&&key!=9&&key!=37&&key!=39){
e、 预防默认值();
}
}
});

您正在哪个浏览器中测试此功能?为什么不使用
type=“number”
?[您仍然需要支持缺乏支持的浏览器。]@epascarello即使我为number定义了输入类型,它仍然允许输入单词字符。(我是用firefox开发的,但我相信大多数浏览器都是一样的)我知道,但这是一家相对较小的公司(30名员工)的内部应用程序,所以我告诉他们不要使用IE。另外,在将不需要的字符插入数据库之前,我会删除PHP中的字符,但正如我所说,我只是想了解JS处理这些事情的方式。谢谢谢谢是的,我同意用户输入的变化,我觉得这非常烦人,但我只是尝试了一些不同的事情,我觉得onkeydown和addEventListener(“keydown”)的行为不一样很奇怪——stackoverflow让我等3分钟接受你的答案,但我会的
// prevent users from typing alpha/ symbol characters on select fields
$("#modal-region").on("keydown", "#markdown, #sku", function(e) {

    var key = e.which;
    // when a keydown event occurs on the 0-9 keys the value 
    // of the "which" property is between 48 - 57 
    // therefore anything with a value greater than 57 is NOT a numeric key

    if ( key > 57) {
        e.preventDefault();

    } else if (key < 48) {

    // we don't want to disable left arrow (37), right arrow (39), delete (8) or tab (9)
    // otherwise the use cannot correct their entry or tab into the next field!

        if (key != 8 && key != 9 && key != 37 && key != 39 ) {
            e.preventDefault();
        }
    }

});