我想检查用户输入是否是JavaScript的电子邮件地址,然后将其发送到服务器或试图发送电子邮件,以防止最基本的误解。


当前回答

// Try this regular Expression by ES6 function

const emailValidate = (email) => {
  const regexp= /^[\w.%+-]+@[\w.-]+\.[\w]{2,6}$/;
  return regexp.test(email);
}

其他回答

JavaScript 可以匹配一个常规表达式:

emailAddress.match( / some_regex /);

下面是RFC22电子邮件的常规表达式:

^((?>[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+\x20*|"((?=[\x01-\x7f])[^"\\]|\\[\x01-\x7f])*
"\x20*)*(?<angle><))?((?!\.)(?>\.?[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+)+|"((?=[\x01-\x
7f])[^"\\]|\\[\x01-\x7f])*")@(((?!-)[a-zA-Z\d\-]+(?<!-)\.)+[a-zA-Z]{2,}|\[(((?(?<
!\[)\.)(25[0-5]|2[0-4]\d|[01]?\d?\d)){4}|[a-zA-Z\d\-]*[a-zA-Z\d]:((?=[\x01-\x7f])
[^\\\[\]]|\\[\x01-\x7f])+)\])(?(angle)>)$

我认为这是最好的解决方案:

/^[^\s@]+@[^\s@]+\.[^\s@]+$/

它允许以下格式:

1.  prettyandsimple@example.com
2.  very.common@example.com
3.  disposable.style.email.with+symbol@example.com
4.  other.email-with-dash@example.com
9.  #!$%&'*+-/=?^_`{}|~@example.org
6.  "()[]:,;@\\\"!#$%&'*+-/=?^_`{}| ~.a"@example.org
7.  " "@example.org (space between the quotes)
8.  üñîçøðé@example.com (Unicode characters in local part)
9.  üñîçøðé@üñîçøðé.com (Unicode characters in domain part)
10. Pelé@example.com (Latin)
11. δοκιμή@παράδειγμα.δοκιμή (Greek)
12. 我買@屋企.香港 (Chinese)
13. 甲斐@黒川.日本 (Japanese)
14. чебурашка@ящик-с-апельсинами.рф (Cyrillic)

它是显而易见的多元化,并允许所有重要的国际角色,同时仍然执行基本的 anything@anything.anything格式,它将阻止技术上允许的空间,但它们是如此罕见,我很高兴做到这一点。

我的常规表达的知识并不那么好,这就是为什么我首先用简单的常规表达来检查整体合成,然后检查其他功能的更具体选项,这可能不是最好的技术解决方案,但这就是为什么我更灵活、更快的方式。

我遇到的最常见的错误是空间(特别是在开始和结束),有时是双点。

function check_email(val){
    if(!val.match(/\S+@\S+\.\S+/)){ // Jaymon's / Squirtle's solution
        // Do something
        return false;
    }
    if( val.indexOf(' ')!=-1 || val.indexOf('..')!=-1){
        // Do something
        return false;
    }
    return true;
}

check_email('check@thiscom'); // Returns false
check_email('check@this..com'); // Returns false
check_email(' check@this.com'); // Returns false
check_email('check@this.com'); // Returns true

这个问题比一眼看起来更难回答。

世界各地有大量的人正在寻找“统治他们所有”,但事实是,有大量的电子邮件提供商。

为什么? 根据 RFC: https://en.wikipedia.org/wiki/Email_address#RFC_specification。

The local-part of the email address may use any of these ASCII characters:

- uppercase and lowercase Latin letters A to Z and a to z;
- digits 0 to 9;
- special characters !#$%&'*+-/=?^_`{|}~;
- dot ., provided that it is not the first or last character unless quoted, and provided also that it does not appear consecutively unless quoted (e.g. John..Doe@example.com is not allowed but "John..Doe"@example.com is allowed);[6]
Note that some mail servers wildcard local parts, typically the characters following a plus and less often the characters following a minus, so fred+bah@domain and fred+foo@domain might end up in the same inbox as fred+@domain or even as fred@domain. This can be useful for tagging emails for sorting, see below, and for spam control. Braces { and } are also used in that fashion, although less often.
- space and "(),:;<>@[\] characters are allowed with restrictions (they are only allowed inside a quoted string, as described in the paragraph below, and in addition, a backslash or double-quote must be preceded by a backslash);
- comments are allowed with parentheses at either end of the local-part; e.g. john.smith(comment)@example.com and (comment)john.smith@example.com are both equivalent to john.smith@example.com.

A__z/J0hn.sm{it!}h_comment@example.com.co

如果你尝试这个地址我赌它将失败在所有的或大部分的regex发布在整个网络上. 但记住这个地址遵循RFC规则,所以它是公平有效的。

想象一下我的失望,因为我无法在任何地方注册与这些 regex 检查!!!

如何处理,是的?

"very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com

在这样做时,一个好做法是“重新输入您的电子邮件”输入,以避免用户输入错误,如果这对您来说不够,请添加一个提前提交的模型窗口,标题为“这是您的当前电子邮件吗?”然后用户输入的电子邮件在一个H2标签中,您知道,清楚地显示他们输入的电子邮件,然后一个“是的,提交”按钮。

最好的一个 :D(RFC友好和没有错误“太复杂”) :

function    isMail(mail)
{
    pattuser = /^([A-Z0-9_%+\-!#$&'*\/=?^`{|}~]+\.?)*[A-Z0-9_%+\-!#$&'*\/=?^`{|}~]+$/i;
    pattdomain = /^([A-Z0-9-]+\.?)*[A-Z0-9-]+(\.[A-Z]{2,9})+$/i;

    tab = mail.split("@");
    if (tab.length != 2)
        return false;
    return (pattuser.test(tab[0]) && pattdomain.test(tab[1]));
}