Javascript 禁用输入字段中的某些字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14806200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 17:58:35  来源:igfitidea点击:

Disable some characters from input field

javascriptjqueryhtml

提问by Nutic

I have found some instructions similar, but none helps, all of them are either for special chars or for digits.

我发现了一些类似的说明,但没有任何帮助,它们都是用于特殊字符或数字的。

I need to make user type only 1 , 2 , 3, 4, 5, N, O, A, B, C . all other characters should be restricted.

我只需要让用户输入 1 , 2 , 3, 4, 5, N, O, A, B, C 。所有其他字符都应受到限制。

Any way I can make my own selection of restricted/allowed chars for inputs ?

有什么办法可以让我自己选择输入的受限/允许字符?

  • Not very familiar with javascript.
  • 对javascript不是很熟悉。

采纳答案by Pragnesh Chauhan

Try this

尝试这个

$(function(){
  $('#txt').keypress(function(e){
    if(e.which == 97 || e.which == 98 || e.which == 99 || e.which == 110 || e.which == 111 || e.which == 65 || e.which == 66 || e.which == 67 || e.which == 78 || e.which == 79 || e.which == 49 || e.which == 50 || e.which == 51 || e.which == 52 || e.which == 53){
    } else {
      return false;
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='text' id='txt' value='' onpaste="return false" />

Update 9th March 2018

2018 年 3 月 9 日更新

$(function(){
  $('#txt').keypress(function(e){
    // allowed char: 1 , 2 , 3, 4, 5, N, O, A, B, C
    let allow_char = [97,98,99,110,111,65,66,67,78,79,49,50,51,52,53];
    if(allow_char.indexOf(e.which) !== -1 ){
      //do something
    }
    else{
      return false;
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='text' id='txt' value='' onpaste="return false" />

回答by allenhwkim

with JQuery,

使用 JQuery,

$("input").keypress( function(e) {
    var chr = String.fromCharCode(e.which);
    if ("12345NOABC".indexOf(chr) < 0)
        return false;
});

without JQuery

没有 JQuery

document.getElementById("foo").onkeypress = function(e) {
    var chr = String.fromCharCode(e.which);
    if ("12345NOABC".indexOf(chr) < 0)
        return false;
};

For one liners, from @mplungjan and @matthew-lock's comment

对于一个班轮,来自@mplungjan 和@matthew-lock 的评论

document.querySelector("#foo").onkeypress = function(e) {
    return "12345NOABC".indexOf(String.fromCharCode(e.which)) >= 0;
};

回答by andydavies

It's not 100% clear whether you want to restrict the form being submitted with invalid values, or literally prevent the user from even typing those values. The other answers deal with the latter, and I think that iswhat you meant, but there will be people arriving here (like me) who just want to prevent form submission.

是否要限制使用无效值提交的表单,或者从字面上阻止用户甚至输入这些值,并不是 100% 清楚的。其他答案涉及后者,我认为这就是您的意思,但是会有人(像我一样)来到这里只是想阻止表单提交。

In which case:

在这种情况下:

Use the patternattribute on the inputelement:

使用元素pattern上的属性input

<input pattern="[REGEX HERE]">

https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms/Form_validation

https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms/Form_validation

https://regex101.com/

https://regex101.com/

回答by Abdul Malik

If you want to disable a few characters from input field you can do something like that:

如果您想从输入字段中禁用几个字符,您可以执行以下操作:

<input type="text" onkeydown="return (event.keyCode!=86);"/>

86 is the code for V. Check code for other keys from the following link.

86 是 V 的代码。从以下链接检查其他键的代码。

https://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes

https://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes

回答by Armaniimus

html

html

<input type="text" name="" value="" id="testr">
<script type="text/javascript" src="testkeydown.js"></script>

javascript

javascript

document.getElementById('testr').addEventListener('keydown', function(e) {
    const regex = RegExp('[0-9a-zA-Z]');

    if (!regex.test(e.key) && e.key != 'backspace') {
        e.preventDefault();
    }
});

In the code example above here you see a piece of code that disables most characters that are not between 0-9, a-z, A-Z in plain js.

在上面的代码示例中,您会看到一段代码禁用了普通 js 中不在 0-9、az、AZ 之间的大多数字符。

If you like to research it more you can google mdn keydown event and look for the Morzilla developer network.

如果你想更多地研究它,你可以谷歌 mdn keydown 事件并寻找 Morzilla 开发者网络。