简体   繁体   English

如何使用jQuery指定文本框中允许的字符?

[英]How to specify which characters are allowed in a textbox with jQuery?

http://jsfiddle.net/WhP8q/ http://jsfiddle.net/WhP8q/

I'm trying to restrict input to alpha numeric, 0-9, AZ,az. 我试图将输入限制为字母数字,0-9,AZ,az。

The ASCII table i'm referencing: http://www.asciitable.com/ Here is what I have so far 我正在参考的ASCII表: http : //www.asciitable.com/这是我到目前为止所拥有的

$(function() {
    $("input").bind("keydown paste", function(e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        var c = code;
        var letterAllowed = ((c > 47 && c < 58) || (c > 64 && c < 90) || (c > 96 && c < 123))
        if (code > 32 && !letterAllowed) {
            return false;
        }
    });
});​

right now, the tilde (~) character is prevented from getting input into the field, but other special / shift characters such as !@#$% all get entered into the text field. 现在,阻止了波浪号(〜)字符无法输入到字段中,但是其他特殊的/移位字符,例如!@#$%都输入了文本字段中。 I'm pretty sure my logic is sound, but my issue is with some misunderstanding of javascript bindings? 我很确定我的逻辑是正确的,但是我的问题是对javascript绑定有一些误解? idk idk

Preventing character input for only some cases is very complicated in javascript, as in the keypress event (the one you'd want to prevent) you do not know the afterwards value of your input, but only the keycode of the pressed key (and not even the resulting char for sure). 在javascript中,仅在某些情况下阻止字符输入非常复杂,因为在keypress事件 (您要阻止的事件)中,您不知道输入的后value ,而仅知道所按下键的keycode (而不是甚至肯定是生成的字符)。 Also, you will need to care about special keys like or . 另外,您将需要注意诸如或的特殊键。

I'd recommend something like this: 我建议这样的事情:

$("input").on("keypress keyup paste", function(e) {
    this.value = this.value.replace(/[^a-zA-Z0-9]/g, '');
});

In case of restrict the character you enter, You can replace the character which is not alphanumberic. 如果您限制输入的字符,则可以替换非字母数字的字符。

<input type='text' id="txtAlphaNumeric"/>
<input type='text' id="txtNumeric"/>
<input type='text' id="txtAlphabet"/>
<script type="text/javascript">
$(function() {
    $('#txtNumeric').keyup(function() {
        if (this.value.match(/[^0-9]/g)) {
            this.value = this.value.replace(/[^0-9]/g, '');
        }
    });
    $('#txtAlphabet').keyup(function() {
        if (this.value.match(/[^a-zA-Z]/g)) {
            this.value = this.value.replace(/[^a-zA-Z]/g, '');
        }
    });
    $('#txtAlphaNumeric').keyup(function() {
        if (this.value.match(/[^a-zA-Z0-9]/g)) {
            this.value = this.value.replace(/[^a-zA-Z0-9]/g, '');
        }
    });
});
</script>

Answer taken from: jquery allow only alphanumeric 答案取自: jQuery只允许字母数字

Turns out, I need to do the following: 原来,我需要执行以下操作:

$("input").keypress(function(e){
        var code = e.charCode;

charCode will give the actual character code of the typed letter, rather than the ascii code of the last pressed key charCode将给出键入字母的实际字符代码,而不是最后一次按键的ASCII代码

see http://api.jquery.com/keypress/ 参见http://api.jquery.com/keypress/

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

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