-1

I am attempting to validate email addresses, however I want the most lenient validation possible as I intend to back this up by sending the user a validation email (I am aware this gets asked a lot but the other questions are focused on being as strict as possible whereas I am attempting to identify the most lenient checks possible).

I still think it's important to have some level of validation to remove things that couldn't possibly be an email address... I don't want "this is not @n email. fool" sitting smugly in my database pretending to be an email. Although I am quite happy to have "this.is.not.an.email@fool.com".

Here is my function so far:

function validate(email) {
  var atIndex = email.lastIndexOf('@');
  // Make sure email contains an '@' character and that it is neither the first or last character
  if (atIndex > 0 && atIndex < email.length -1) {
    // Everything before the last '@' character
    var local = email.substring(0, atIndex);
    // Everything after the last '@' character
    var domain = email.substring(atIndex + 1, email.length);
    var dotIndex = domain.lastIndexOf('.');

    // Make sure domain contains a '.' character and that it is neither the first or last character
    if (dotIndex > 0 && dotIndex < domain.length - 1) {
      // Array of strings that aren't allowed to appear in a domain
      var domainRestrictions = [
        "..",
        " "
      ];
      var i = domainRestrictions.length;
      while (i-- > -1) {
        if (domain.indexOf(domainRestrictions[i]) > -1) {
          return false;
        }
      }
      // Array of strings that the local portion can neither start or end with
      var localRestrictions = [
        ".",
        " "
      ];
      i = localRestrictions.length;
      while (i-- > -1) {
        var string = localRestrictions[i];
        if (local.indexOf(string) == 0 || local.lastIndexOf(string) == local.length - 1) {
          return false;
        }
      }

      return true;
    }

  }
  return false;
}

Currently I disallow the following:

  • Anything without an '@' symbol.
  • Any domain not containing a '.' or that contains it as the first or last character.
  • Any domain containing whitespace or '..' Any local section starting or ending with '.' or whitespace

Everything else is considered valid and passed on.

My question is, are there any valid email addresses this will choke on? Are there any more safe assumptions I can make that an email address can't contain?

4

3 回答 3

1

如果您绝对打算拥有一个 100% 有效的电子邮件地址,那么对于初学者,我建议您阅读 RFC 2822,它可以在https://www.rfc-editor.org/rfc/rfc2822#section-3.4.1找到。此规范的完整实现将确保输入的所有电子邮件地址都采用完全有效的格式。这远远超出了除了最复杂的正则表达式之外的所有内容——例如,您可能会发现您需要处理西里尔文、希腊文或 Unicode 字符集。

然而 ...

与您节省的时间相比,实施此规范将花费大量时间。即使电子邮件地址的格式仍然有效,仍然存在一些问题,包括:

  • 域名可能未注册;
  • 该域可能没有 MX 记录;
  • 域可能没有 A 记录,作为后备;或者,
  • 用户可能实际上并不存在。

坦率地说,与其花时间确保电子邮件地址严格遵守正确的格式,不如花时间确保它“足够好”并专注于验证过程的其他方面。

于 2013-08-12T11:03:20.840 回答
0

请在以下位置查看详尽的规则集 -

http://rumkin.com/software/email/rules.php

于 2013-08-12T11:03:50.510 回答
0

如果你使用正则表达式,你会少很多麻烦。有一些电子邮件验证模式可以验证您的电子邮件地址。

Pattern pattern = Pattern.compile("([A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4})?");
Matcher matcher = pattern.matcher(yourEmailAddress);
if(matcher.matches()){
  //do something
}else {
  //tell the user it didn't match
}
于 2013-08-12T11:02:22.317 回答