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


当前回答

如果您正在使用 Closure 您可以使用内置的 goog.format.Email 地址类型:

http://docs.closure-library.googlecode.com/git/class_goog_format_EmailAddress.html

例如:

goog.format.EmailAddress.isValidAddrSpec("blah@blah.com")

请注意,通过阅读来源(上面的链接),您可以看到评论表示 IDN 不受支持,并且仅旨在覆盖大多数地址:

// This is a fairly naive implementation, but it covers 99% of use cases.
// For more details, see http://en.wikipedia.org/wiki/Email_address#Syntax
// TODO(mariakhomenko): we should also be handling i18n domain names as per
// http://en.wikipedia.org/wiki/Internationalized_domain_name

其他回答

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

这是一个节点和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');
            }
        });
}

这是由数千个网站使用的官方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

使用浏览器/运行时间来处理通过预付一个协议并将其转移到URL API,捕捉任何错误并检查结果的用户名和主名属性。

function validEmail(email=''){
    var $0, url, isValid = false, emailPatternInput = /^[^@]{1,64}@[^@]{4,253}$/, emailPatternUrl = /^[^@]{1,64}@[a-z][a-z0-9\.-]{3,252}$/i;
    email = email.trim();
    try{
        url = new URL('http://'+email);
        $0 = `${url.username}@${url.hostname}`;
        isValid = emailPatternInput.test( email );
        if(!isValid) throw 'invalid email pattern on input:' + email;
        isValid = emailPatternUrl.test( $0 );
        if(!isValid) throw 'invalid email pattern on url:' + $0;
        console.log(`email looks legit "${email}" checking url-parts: "${$0 === email ? '-SAME-':$0}"`);
    }catch(err){
        console.error(`probably not an email address: "${email}"`, err);
    };
    return isValid;
}

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

这是我可以提出的最简单和最一般允许的例子,请在可以更准确的情况下编辑,同时保持其简单性和合理的一般允许有效性。

此外,请参见 MDN URL 文件 URL, window.URL 和 Nodejs for URL APIs。

我正在使用这个功能

/**
 * @param {*} email
 */
export const validateEmail = email => {
    return new RegExp(/[\w-]+@([\w-]+\.)+[\w-]+/gm).test(email);
};
 <input type="email" class="form-control" required="required" placeholder="Email Address" name="Email" id="Email" autocomplete="Email">
 <button class="btn-1 shadow-0 full-width" type="button" id="register">Register account</button>

 $("#register").click(function(){       
    var rea = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
    var Email = $("#Email").val();
    var x = rea.test(Email);
    if (!x) {
        alert('Type Your valid Email');
        return false;
    }           
 </script>