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


当前回答

Rnevius的(和同事)解决方案是辉煌的,但它允许添加Cyrillic,日本语,Emoticons和其他单码符号,一些服务器可能受到限制。

<unk> <unk> <unk> <unk> <unk> <unk> <unk> <unk>

<unk> <unk> <unk> <unk> <unk> <unk> <unk> <unk>

其他回答

我写了一个JavaScript电子邮件验证器,完全兼容PHP的filter_var($值,FILTER_VALIDATE_EMAIL)实施。

https://github.com/mpyw/FILTER_VALIDATE_EMAIL.js

import validateEmail from 'filter-validate-email'

const value = '...'
const result = validateEmail(value)

相当于:

<?php

$value = '...';
$result = (bool)filter_var($value, FILTER_VALIDATE_EMAIL, FILTER_FLAG_EMAIL_UNICODE);

如何创建一个功能,将测试任何字符串对电子邮件的模式使用常规表达在JavaScript,因为我们知道电子邮件地址可以是相当不同的地区,如在英国和澳大利亚它通常以.co.uk 或.com.au 结束,所以我试图覆盖这些也,也检查字符串是否转移到功能,这样的东西:

var isEmail = function(str) {
  return typeof str==='string' && /^[\w+\d+._]+\@[\w+\d+_+]+\.[\w+\d+._]{2,8}$/.test(str);
}

然后检查它是否像下面的电子邮件:

isEmail('alex@example.com'); //true
isEmail('alireza@test.co.uk'); //true
isEmail('peter.example@yahoo.com.au'); //true
isEmail('alex@example.com'); //true
isEmail('peter_123@news.com'); //true
isEmail('hello7___@ca.com.pt'); //true
isEmail('example@example.co'); //true
isEmail('hallo@example.coassjj#sswzazaaaa'); //false
isEmail('hallo2ww22@example....caaaao'); //false

使用 JavaScript 中的 URL 接口将地址分成最小实用的预期格式 user@host 然后检查它看起来合理。 接下来发送一个消息,看看它是否工作(例如,需要接收者通过地址验证一次标志)。 请注意,这处理处罚代码,国际化,如下示例所示。

https://developer.mozilla.org/en-US/docs/Web/API/URL

一个简单的测试:

function validEmail(input=''){
    const emailPatternInput = /^[^@]{1,64}@[^@]{4,253}$/, emailPatternUrl = /^[^@]{1,64}@[a-z][a-z0-9\.-]{3,252}$/i;
    let email, url, valid = false, error, same = false;
    try{
        email = input.trim();
        // handles punycode, etc using browser's own maintained implementation
        url = new URL('http://'+email);
        let urlderived = `${url.username}@${url.hostname}`;
        same = urlderived === email;
        valid = emailPatternInput.test( email );
        if(!valid) throw new Error('invalid email pattern on input:' + email);
        valid = emailPatternUrl.test( urlderived );
        if(!valid) throw new Error('invalid email pattern on url:' + urlderived);
    }catch(err){
        error = err;
    };
    return {email, url, same, valid, error};
}

[
 'user+this@はじめよう.みんな'
, 'stuff@things.eu'
, 'stuff@things'
, 'user+that@host.com'
, 'Jean+François@anydomain.museum','هيا@יאללה'
, '试@例子.测试.مثال.آزمایشی'
, 'not@@really'
, 'no'
].forEach(email=>console.log(validEmail(email), email));

您可以使用此 regex (从 w3 资源(与 W3C 无关):

/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailValue)

如果您使用 Node,您可以在后端以及前端使用此功能。

我不知道其他背景语言,所以我不能对其他使用情况进行评估。

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

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

为什么? 根据 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标签中,您知道,清楚地显示他们输入的电子邮件,然后一个“是的,提交”按钮。