[英]input only allow english and non-english character javascript?
How can I make input box for name only allow both E and non-E character, not special character on keydown event.如何使名称的输入框只允许 E 和非 E 字符,而不是 keydown 事件中的特殊字符。
For example: Accept: a â è ㅎ 啊 Deny: .例如: 接受:a è ㅎ 啊 拒绝:。 \\ | \\ | } ) # + } ) # +
And also I don't know why:而且我也不知道为什么:
'.'.charCodeAt(0) = 46 (chrome console)
In my keydown event if I type dot (.)在我的 keydown 事件中,如果我输入点 (.)
e.which = 190
if I type dot (.) in numlock keyboard如果我在 numlock 键盘中输入点 (.)
e.which = 110
so confuse.所以混淆。
Try this.尝试这个。 Seems it works fine with jQuery 1.4.2, FF, IE, Chrome.似乎它适用于 jQuery 1.4.2、FF、IE、Chrome。
<script>
$("input").keypress(function (e) {
if (e.which !== 0 &&
!e.ctrlKey && !e.metaKey && !e.altKey) {
alert(String.fromCharCode(e.which));
}
});
</script>
You can use this regex to replace all special characters from String :您可以使用此正则表达式替换 String 中的所有特殊字符:
String.replace(/[\u0021-\u002e\u003a-\u0040\u005b-\u0060\u007b-\u007e]*/g, "")
Similarly you can use this regex to find if user's entered text matches the regex and return false.同样,您可以使用此正则表达式来查找用户输入的文本是否与正则表达式匹配并返回 false。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.