javascript jQuery 电子邮件正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8112329/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 02:20:36  来源:igfitidea点击:

jQuery Email Regex

javascriptjqueryregex

提问by Oyed

I have been developing a script and I'm stumped right now. I have tried many different Regex from Google and non have worked, this is my code:

我一直在开发一个脚本,现在我很难过。我尝试了许多来自 Google 的不同正则表达式,但都没有奏效,这是我的代码:

$(document).ready(function() {
    var email_check = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$/;
    num = 0;
    $('#email').focus(function() {
        $('#email_check').show();
        $(this).keyup(function() {
            error = 0;
            var email = $('#email').val();
            num = num+1;
            if(!email_check.test(email)) {
                error = 1;
            }
            if(error == 0) {
                $('#email_check').html('O'+num+email+error);
            }else if(error == 1) {
                $('#email_check').html('X'+num+email+error);
            }
        });
    });
});

Any help is great, thanks!

任何帮助都很棒,谢谢!

回答by circusbred

Set the case insensitive flag (i):

设置不区分大小写的标志 (i):

var email_check = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$/i;

回答by Jason Gritman

You could skip writing the regex and use the jQuery validation pluginfor this task. There's an example hereof validating an email address.

您可以跳过编写正则表达式并使用jQuery 验证插件来完成此任务。这里有一个例子在这里验证的电子邮件地址。