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

时间:2015-06-23 23:04:56

标签: php validation zend-framework

如何使此无效(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??????????
}    

1 个答案:

答案 0 :(得分:4)

ZF说他们是有效的,因为从技术上来说,他们是。

根据RFC 2822 - 互联网邮件格式,地址属于addr-spec格式(在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()返回true后添加额外的支票,以检查是否存在您认为不允许在电子邮件地址中使用的任何特殊字符。< / p>