简体   繁体   English

只允许空格、字母、退格和制表符

[英]Allow only white space, letters, backspace, and tab

I'm using Firefox to test my script out.我正在使用 Firefox 来测试我的脚本。 My goal is to allow white space, letters, backspace and tab to work, but for some reason I could break my code and type in a number which I don't want.我的目标是允许空格、字母、退格键和制表符工作,但出于某种原因,我可能会破坏我的代码并输入我不想要的数字。 How can I fix this issue?我该如何解决这个问题?

This is what I have so far:这是我到目前为止:

                $(document).ready(function () {
                    $("#inputTextBox").keypress(function (event) {
                        var inputValue = event.which;

                        //Backspace ASCII = 8 
                        //Tab ASCII = 9 
                        if (!(inputValue >= 65 && inputValue <= 123)) {
                            if (inputValue === 8 && inputValue === 9){
                                event.preventDefault();
                            }
                        }
                        console.log(inputValue);
                    });

if (inputValue === 8 && inputValue === 9)这个条件永远不会通过,你的 var 不能同时等于 8 和 9,你应该使用 ||

This is what worked for me.这对我有用。 This allows letters, white space, backspace and tab.这允许字母、空格、退格和制表符。 I'm going to leave it here in case someone needs it.我会把它留在这里以防有人需要它。

                $(document).ready(function () {
                    $("#inputTextBox").keypress(function (event) {
                        var inputValue = event.which;
                        //Allow letters, white space, backspace and tab. 
                        //Backspace ASCII = 8 
                        //Tab ASCII = 9 
                        if (!(inputValue >= 65 && inputValue <= 123)
                            && (inputValue != 32 && inputValue != 0)
                            && (inputValue != 48 && inputValue != 8)
                            && (inputValue != 9)){
                                event.preventDefault(); 
                        }
                        console.log(inputValue);
                    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM