只允许英文字符和数字输入文字

Ger*_*tod 12 javascript validation jquery input alphanumeric

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

如何更改此选项,以便输入仅在键入时允许字符AZ,az,0-9,而不使用正则表达式?

Pau*_*aul 34

假设你也想接受空格:

$("#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;
});
Run Code Online (Sandbox Code Playgroud)

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

的jsfiddle


小智 7

<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)" />
Run Code Online (Sandbox Code Playgroud)

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


moh*_*ari 7

所有其他答案都很好,但不会阻止复制和粘贴非英语字符。

\n

\xe2\x80\x8cBellow 代码适用于两者(按键复制粘贴):

\n
<input type="text" name="text" oninput="this.value=this.value.replace(/[^A-Za-z\\s]/g,'');">\n
Run Code Online (Sandbox Code Playgroud)\n