Javascript 只允许英文字符和数字进行文本输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7144167/
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 01:01:59  来源:igfitidea点击:

only allow English characters and numbers for text input

javascriptjqueryinputvalidationalphanumeric

提问by Gereltod

Live Demo: http://jsfiddle.net/thisizmonster/DveuB/

现场演示:http: //jsfiddle.net/thisizmonster/DveuB/

How can I change this so that the input only allows the characters A-Z, a-z, 0-9 while typing, without using a regular expression?

如何更改此设置,以便在输入时只允许输入字符 AZ、az、0-9,而不使用正则表达式?

回答by Paul

Assuming you also want to accept spaces:

假设您还想接受空格:

$("#user").keypress(function(event){
    var ew = event.which;
    if(ew == 32)
        return true;
    if(48 <= ew && ew <= 57)
        return true;
    if(65 <= ew && ew <= 90)
        return true;
    if(97 <= ew && ew <= 122)
        return true;
    return false;
});

If you don't want to accept spaces then remove the if(ew == 32) return true;

如果您不想接受空格,请删除 if(ew == 32) return true;

JSFiddle

JSFiddle

回答by Siya Qalistic

<input type="text" id="firstName"  onkeypress="return (event.charCode >= 65 && event.charCode <= 90) || (event.charCode >= 97 && event.charCode <= 122) || (event.charCode >= 48 && event.charCode <= 57)" />

The ASCII Character Set : https://www.w3schools.com/charsets/ref_html_ascii.asp

ASCII 字符集:https: //www.w3schools.com/charsets/ref_html_ascii.asp

回答by 321X

You can do something like this: http://jsfiddle.net/DveuB/1/

你可以这样做:http: //jsfiddle.net/DveuB/1/

Then it's only 0-9, a-z and A-Z

然后就只有0-9了,az和AZ

$(function(){
    $("#user").keypress(function(event){
        if ((event.charCode >= 48 && event.charCode <= 57) || // 0-9
            (event.charCode >= 65 && event.charCode <= 90) || // A-Z
            (event.charCode >= 97 && event.charCode <= 122))  // a-z
            alert("0-9, a-z or A-Z");
    });
});

Update:http://jsfiddle.net/DveuB/4/
To prevent what @mu is talking about:

更新:http : //jsfiddle.net/DveuB/4/
为了防止@mu 在说什么:

$("#user").keyup(function(event){
    if (event.altKey == false && event.ctrlKey == false)
        if ((event.keyCode >= 48 && event.keyCode <= 57 && event.shiftKey== false) ||
            (event.keyCode >= 65 && event.keyCode <= 90) ||
            (event.keyCode >= 97 && event.keyCode <= 122))
            alert("0-9, a-z or A-Z");
});

回答by SajithG

<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(document).ready(function () {

  $('.clsAlphaNoOnly').keypress(function (e) {  // Accept only alpha numerics, no special characters 
        var regex = new RegExp("^[a-zA-Z0-9 ]+$");
        var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
        if (regex.test(str)) {
            return true;
        }

        e.preventDefault();
        return false;
    }); 
})
</script>

<body>

<input class='clsAlphaNoOnly' type='text'>
</body>
</html>

回答by Vladimir Ishenko

I'm using this function.

我正在使用这个功能。

By modifying RegExp you can get rid of any characters you don't like personally.

通过修改 RegExp,您可以删除您个人不喜欢的任何字符。

$(function(){
    $("#user").bind('keypress',function(e){
        var regex = new RegExp("^[a-zA-Z0-9 ]+$");
        var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
        if (regex.test(str)) return true;
        e.preventDefault();
        return false;
    });
});

回答by Bas Slagter

Think you have to write some if statements or something like that, that takes the keycode and validates it against some numbers that represent other keycodes:

认为您必须编写一些 if 语句或类似的东西,它采用键码并根据代表其他键码的一些数字对其进行验证:

for example: if(keycode < 80) return false;

例如: if(keycode < 80) return false;