使用 javascript 和 jquery 验证按键时的电子邮件地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7165976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Validating an email address on key-press using javascript & jquery
提问by Prisoner ZERO
I'm trying to prevent users from inputting invalid characters when typing-in an email address. I am NOTtrying to validate the entire value as-a-whole (that happens later).
我试图防止用户在输入电子邮件地址时输入无效字符。我不是要从整体上验证整个值(稍后会发生)。
I "thought" this was the correct way to prevent a list of characters (such as # or $):
我“认为”这是防止字符列表(例如 # 或 $)的正确方法:
/[A-Z0-9a-z@]^[$#<>?]/
This part works:
这部分工作:
/[A-Z0-9a-z@]/
This part fails:
这部分失败:
/^[$#<>?]/
Any thoughts?
有什么想法吗?
回答by sg3s
Try /[^$#<>?]/
instead.
试试吧/[^$#<>?]/
。
The ^
insideand at directly after the opening bracket makes the class negative.
的^
内部开口支架后和直接使类负。
You can learn more about this on regular-expressions.info
您可以在regular-expressions.info上了解更多相关信息
Or better, heres a workign simple regex that does it, using yours as a start.
或者更好的是,这里有一个简单的正则表达式,可以使用您的作为开始。
回答by ???? ????? ???? ??????
$('input[type=email]').on('keypress', function (e) {
var re = /[A-Z0-9a-z@\._]/.test(e.key);
if (!re) {
return false;
}
});
回答by teenup
You don't need to write all the special characters into the regular expression that need to be prohibited, because there will be a large list, simple write the ones that are allowed because they are less(that you have already done and perhaps only one or two more characters viz. DOT,UnderScore
are remaining).
你不需要把所有需要禁止的特殊字符都写进正则表达式中,因为会有很大的列表,简单写下允许的,因为它们少(你已经做了,也许只有一个)或另外两个字符,即DOT,UnderScore
剩余)。
The code you have used to test the validity of inputted character is already fine, which will verify the character to be out of [A-Z0-9a-z@]
only, probably you can make it [A-Z0-9a-z@\._]
for other two characters.
你用来测试输入字符有效性的代码已经可以了,这将验证字符是否为 [A-Z0-9a-z@]
唯一,可能你可以[A-Z0-9a-z@\._]
为其他两个字符制作它。
keychar = String.fromCharCode(keynum);
regEx = /[A-Z0-9a-z@]/;
return regEx.test(keychar);
回答by CONvid19
<script type="text/javascript">
<!--
var email='1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.@_'
var bksp = 'backspace'
var alt = 'alt'
function alpha(e,allow) {
var k;
k=document.all?parseInt(e.keyCode): parseInt(e.which);
return (allow.indexOf(String.fromCharCode(k))!=-1);
}
// -->
</script>
<input type="text" onkeypress="return alpha(event,email+bksp+alt)" />
回答by Prisoner ZERO
FIXED:
The problem was some keynum values collide with undesirable characters. This fixed it!
修正:问题是一些 keynum 值与不需要的字符发生冲突。这修复了它!
// **********************
// .emailOnly
// Use -
// Makes the element email-only.
//
// Example -
// $('.myElement').emailOnly();
// **********************
(function($) {
$.fn.extend({
emailOnly: function() {
return this.each(function() {
return $(this).keypress(function(e, text) {
var keynum;
var keychar;
var regEx;
var allowedKeyNums = [8, 9, 35, 36, 46]; // Backspace, Tab, End, Home, (Delete & period)
if (window.event) // IE
keynum = e.keyCode;
else if (e.which) // Netscape/Firefox/Opera
keynum = e.which;
else
keynum = e.keyCode
keychar = String.fromCharCode(keynum);
regEx = /[^$#<>?]/ // Undesirable characters
// Test for keynum values that collide with undesirable characters
if ($.inArray(keynum, allowedKeyNums) > -1)
return regEx.test(keychar);
regEx = /[A-Z0-9a-z@]/
return regEx.test(keychar);
});
});
}
});
})(jQuery);