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


当前回答

Regex 确认电子邮件地址

[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])+

其他回答

我知道它不是雷格斯,但以任何方式......

这是一个节点和npm包电子邮件存在的例子,这是最终检查电子邮件是否存在,如果它在正确的形式:)

这将粘贴电子邮件,如果它的回复,如果它没有回复,它将返回虚假或其他真实。

function doesEmailExist(email) {
    var emailExistence = require('email-existence');
    return emailExistence.check(email,function (err,status) {
            if (status) {
                return status;
            }
            else {
                throw new Error('Email does not exist');
            }
        });
}

以下是 Regex 认证:

没有 @(-) 和(.) 之前的空间字符不应该在 @ 之后聚集在一起 没有 @ 2 字符之后的特殊字符必须在 @ 电子邮件之前的长度不应该低于 128 字符

function validateEmail(email) {
var chrbeforAt = email.substr(0, email.indexOf('@'));
if (!($.trim(email).length > 127)) {
    if (chrbeforAt.length >= 2) {
        var re = /^(([^<>()[\]{}'^?\\.,!|//#%*-+=&;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
        //var re = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
        return re.test(email);
    } else {
        return false;
    }
} else {
    return false;
}
}

在您的验证函数中使用此代码:

var emailID = document.forms["formName"]["form element id"].value;
atpos = emailID.indexOf("@");
dotpos = emailID.lastIndexOf(".");
if (atpos < 1 || ( dotpos - atpos < 2 ))
{
    alert("Please enter correct email ID")
    return false;
}

您可以使用 jQuery. 内部规则定义:

eMailId: {
    required: true,
    email: true
}

这是由数千个网站使用的官方Rails指南提出的验证的JavaScript翻译:

/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i

相对简单,但对大多数常见的错误进行测试。

测试了数千个电子邮件的数据集,并且有零的虚假负面 / 积极。

使用例子:

const emailRegex = /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i;

emailRegex.test('email@example.com');    // true

// Multi-word domains
emailRegex.test('email@example.co.uk');  // true
emailRegex.test('email@mail.gmail.com'); // true

// Valid special characters
emailRegex.test('unusual+but+valid+email1900=/!#$%&\'*+-/=?^_`.{|}~@example.com') // true

// Trailing dots
emailRegex.test('email@example.co.uk.'); // false

// No domain
emailRegex.test('email@example');        // false

// Leading space
emailRegex.test(' email@example.com');   // false

// Trailing space
emailRegex.test('email@example.com ');   // false

// Incorrect domains
emailRegex.test('email@example,com ');   // false

// Other invalid emails
emailRegex.test('invalid.email.com')        // false
emailRegex.test('invalid@email@domain.com') // false
emailRegex.test('email@example..com')       // false

很难得到一个电子邮件验证器100%正确的唯一真正的方式来得到它正确的将是发送一个测试电子邮件到帐户。

一些要改进的事情:

而不是新的RegExp,只需尝试这样写出regexp:

if (reg.test(/@/))

其次,请确保在 @ 标志之后出现一个时期,并确保 @ 和 期间之间有字符。