如何验证文本输入仅包含拉丁字母? [英] How do I validate that a text input contains only latin letters?
本文介绍了如何验证文本输入仅包含拉丁字母?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用jQuery验证(在keyup
上)文本输入仅包含拉丁字母(only_english = 'abcdAbDKDk',
),而不使用插件?
How can I use jQuery to validate (on keyup
) that a text input only contains latin letters (only_english = 'abcdAbDKDk',
), without using a plugin?
推荐答案
尝试如下操作:
$(function() {
$(".latin-only").keypress(function(event) {
if(!((event.which >= 65 && event.which <= 90) ||
(event.which >= 97 && event.which <= 122))) {
event.preventDefault();
}
});
});
如果要允许空格,请将其添加到上面的语句中(|| event.which == 32).当用户将值粘贴到输入中时,您将需要在某处(可能是在更改事件或键盘输入中)使用额外的逻辑来从字符串中删除非法字符.
If you want to allow spaces, add that to the statement above (|| event.which == 32). You will need extra logic somewhere (perhaps in a change event, or keyup) to remove illegal chars from the string when users paste a value into the input.
这篇关于如何验证文本输入仅包含拉丁字母?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文