Zend 框架 - 如何验证电子邮件?

Zend framework - how to validate the email?

如何使这个无效(ZF2)?

$email = a*@gmail.com
$email = a#@gmail.com
$email = a!@gmail.com
$email = a...many symbols...@gmail.com

以上所有电子邮件均显示为使用以下 ZF 方法有效,我希望它变为错误。

$validator = new EmailAddress();    
if ($validator->isValid($email)) {
  // ARE YOU DRUNK???? why a*@gmail.com is true?
} else {
  // WHY NOT??????????
}    

ZF 说它们是有效的,因为从技术上讲它们是有效的。

根据RFC 2822 - Internet Message Format, addresses are of the addr-spec format (defined in section 3.4.1

addr-spec       =       local-part "@" domain
local-part      =       dot-atom / quoted-string / obs-local-part

查看 dot-atom 的组成:

3.2.4. Atom

   Several productions in structured header field bodies are simply
   strings of certain basic characters.  Such productions are called
   atoms.

   Some of the structured header field bodies also allow the period
   character (".", ASCII value 46) within runs of atext.  An additional
   "dot-atom" token is defined for those purposes.

atext           =       ALPHA / DIGIT / ; Any character except controls,
                        "!" / "#" /     ;  SP, and specials.
                        "$" / "%" /     ;  Used for atoms
                        "&" / "'" /
                        "*" / "+" /
                        "-" / "/" /
                        "=" / "?" /
                        "^" / "_" /
                        "`" / "{" /
                        "|" / "}" /
                        "~"

atom            =       [CFWS] 1*atext [CFWS]

dot-atom        =       [CFWS] dot-atom-text [CFWS]

dot-atom-text   =       1*atext *("." 1*atext)

   Both atom and dot-atom are interpreted as a single unit, comprised of
   the string of characters that make it up.  Semantically, the optional
   comments and FWS surrounding the rest of the characters are not part
   of the atom; the atom is only the run of atext characters in an atom,
   or the atext and "." characters in a dot-atom.

如您所见,*#!?等字符都是有效字符。

如果你想认为这些无效,你可以在 EmailAddress::isValid() returns true 之后添加一个额外的检查来检查是否存在任何你认为不允许出现的特殊字符电子邮件地址。