jQuery只允许拉丁字符

时间:2016-12-30 06:09:06

标签: javascript jquery

我想要一个允许类型的js代码只有拉丁包机的数字和特殊字符(Alt键盘序列,EX:std::bind(&A::foo, &a);'`,...),我试过这样,但它对拉丁数和隐形字符不起作用。我该怎么做?

DEMO: https://jsfiddle.net/oosjtmgg/



std::bind(&A::foo, a);

A




2 个答案:

答案 0 :(得分:0)

请试试这个:

只添加: - var englishAlphabetAndWhiteSpace =“/^ [ - @。/#& + \\\\\\\\

$(".latin_letters").on("keypress", function(event) {
        var englishAlphabetAndWhiteSpace = /^[-@./#&+\w\s]*$/;
            var key = String.fromCharCode(event.which);
            if (event.keyCode == 8 || event.keyCode == 37 || event.keyCode == 39 || englishAlphabetAndWhiteSpace.test(key)) {
                return true;
            }
            return false;
        });
        $('.latin_letters').on("paste", function(e) {
            e.preventDefault();
        });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="email" class="latin_letters" type="text">

答案 1 :(得分:0)

&#13;
&#13;
$(".latin_letters").on("keydown", function(event) {
            var allowed = /[A-Za-z0-9!@#\$%\^&\*\(\)_|}{~">:<\?/\]\[\\=\- ]/g;
            var key = String.fromCharCode(event.which);
            if (event.keyCode == 8 || event.keyCode == 37 || event.keyCode == 39 || allowed.test(key)) {
                return true;
            }
            return false;
        });
$('.latin_letters').on("paste", function(e) {
    e.preventDefault();
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="email" class="latin_letters">
&#13;
&#13;
&#13;

这对你有用。你之前没有测试过数字和信号。