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


当前回答

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

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

其他回答

\b[a-z][\w\d_\.]+@\w+\.[a-z]{2}[a-z]?\.?[a-z]{,2}\s

它允许:

abcxyz123@qwert.com    
abc123xyz@asdf.co.in   
abc1_xyz1@gmail1.com   
abc.xyz@gmail.com.in

使用常规表达式可能是最好的方式. 你可以在这里看到大量的测试(从<unk>取出)

const validateEmail = (email) => {
  return String(email)
    .toLowerCase()
    .match(
      /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
    );
};

下面是接受单码的常规表达式的例子:

const re =
  /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;

但是,请记住,我们不应该只依靠JavaScript验证,JavaScript可以轻松禁用,这也应该在服务器侧验证。

下面是行动中的上述例子:

在现代浏览器中,您可以使用纯粹的JavaScript和DOM创建 @Sushil的答案:

function validateEmail(value) {
  var input = document.createElement('input');

  input.type = 'email';
  input.required = true;
  input.value = value;

  return typeof input.checkValidity === 'function' ? input.checkValidity() : /\S+@\S+\.\S+/.test(value);
}

我把一个例子聚集在一个字符串 http://jsfiddle.net/boldewyn/2b6d5/. 结合功能检测和Squirtle的答案的字符串验证,它释放了你从常规的表达屠杀,并不会在旧的浏览器上跳舞。

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

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

我的同事和我分享了这个雷格斯,我很喜欢它。

function isValidEmailAddress (email) {
    var validEmail = false;
    if (email) {
        email = email.trim().toLowerCase();
        var pattern = /^[\w-']+(\.[\w-']+)*@([a-zA-Z0-9]+[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*?\.[a-zA-Z]{2,6}|(\d{1,3}\.){3}\d{1,3})(:\d{4})?$/;
        validEmail = pattern.exec(email);
    }

    return validEmail;
}

if (typeof String.prototype.trim !== 'function') {
    String.prototype.trim = function() {
        return this.replace(/^\s+|\s+$/g, '');
    };
}