只允许英文字符和数字输入文字 [英] only allow English characters and numbers for text input

查看:623
本文介绍了只允许英文字符和数字输入文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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?

推荐答案

假设您还想接受空格:

$("#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(ew) == 32)return true;

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

JSFiddle

这篇关于只允许英文字符和数字输入文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆