Zend_Validate-EmailAddress.xml 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect2 id="zend.validate.set.email_address">
  4. <title>EmailAddress</title>
  5. <para>
  6. <classname>Zend_Validate_EmailAddress</classname> allows you to validate an email address.
  7. The validator first splits the email address on local-part @ hostname and attempts to match
  8. these against known specifications for email addresses and hostnames.
  9. </para>
  10. <sect3 id="zend.validate.set.email_address.basic">
  11. <title>Basic usage</title>
  12. <para>
  13. A basic example of usage is below:
  14. </para>
  15. <programlisting language="php"><![CDATA[
  16. $validator = new Zend_Validate_EmailAddress();
  17. if ($validator->isValid($email)) {
  18. // email appears to be valid
  19. } else {
  20. // email is invalid; print the reasons
  21. foreach ($validator->getMessages() as $message) {
  22. echo "$message\n";
  23. }
  24. }
  25. ]]></programlisting>
  26. <para>
  27. This will match the email address <varname>$email</varname> and on failure populate
  28. <code>$validator->getMessages()</code> with useful error messages.
  29. </para>
  30. </sect3>
  31. <sect3 id="zend.validate.set.email_address.complexlocal">
  32. <title>Complex local parts</title>
  33. <para>
  34. <classname>Zend_Validate_EmailAddress</classname> will match any valid email address
  35. according to RFC2822. For example, valid emails include <code>bob@domain.com</code>,
  36. <code>bob+jones@domain.us</code>, <code>"bob@jones"@domain.com</code> and
  37. <code>"bob jones"@domain.com</code>
  38. </para>
  39. <para>
  40. Some obsolete email formats will not currently validate (e.g. carriage returns or a
  41. "\" character in an email address).
  42. </para>
  43. </sect3>
  44. <sect3 id="zend.validate.set.email_address.hostnametype">
  45. <title>Validating different types of hostnames</title>
  46. <para>
  47. The hostname part of an email address is validated against <link
  48. linkend="zend.validate.set.hostname">
  49. <classname>Zend_Validate_Hostname</classname></link>. By default
  50. only DNS hostnames of the form <code>domain.com</code> are accepted, though if you wish
  51. you can accept IP addresses and Local hostnames too.
  52. </para>
  53. <para>
  54. To do this you need to instantiate <classname>Zend_Validate_EmailAddress</classname>
  55. passing a parameter to indicate the type of hostnames you want to accept. More details
  56. are included in <classname>Zend_Validate_Hostname</classname>, though an example of how
  57. to accept both DNS and Local hostnames appears below:
  58. </para>
  59. <programlisting language="php"><![CDATA[
  60. $validator = new Zend_Validate_EmailAddress(
  61. Zend_Validate_Hostname::ALLOW_DNS |
  62. Zend_Validate_Hostname::ALLOW_LOCAL);
  63. if ($validator->isValid($email)) {
  64. // email appears to be valid
  65. } else {
  66. // email is invalid; print the reasons
  67. foreach ($validator->getMessages() as $message) {
  68. echo "$message\n";
  69. }
  70. }
  71. ]]></programlisting>
  72. </sect3>
  73. <sect3 id="zend.validate.set.email_address.checkacceptance">
  74. <title>Checking if the hostname actually accepts email</title>
  75. <para>
  76. Just because an email address is in the correct format, it doesn't necessarily mean
  77. that email address actually exists. To help solve this problem, you can use MX
  78. validation to check whether an MX (email) entry exists in the DNS record for the
  79. email's hostname. This tells you that the hostname accepts email, but doesn't tell you
  80. the exact email address itself is valid.
  81. </para>
  82. <para>
  83. MX checking is not enabled by default and at this time is only supported by UNIX
  84. platforms. To enable MX checking you can pass a second parameter to the
  85. <classname>Zend_Validate_EmailAddress</classname> constructor.
  86. </para>
  87. <programlisting language="php"><![CDATA[
  88. $validator = new Zend_Validate_EmailAddress(Zend_Validate_Hostname::ALLOW_DNS,
  89. true);
  90. ]]></programlisting>
  91. <para>
  92. Alternatively you can either pass <constant>TRUE</constant> or
  93. <constant>FALSE</constant> to <code>$validator->setValidateMx()</code> to enable or
  94. disable MX validation.
  95. </para>
  96. <para>
  97. By enabling this setting network functions will be used to check for the presence of an
  98. MX record on the hostname of the email address you wish to validate. Please be aware
  99. this will likely slow your script down.
  100. </para>
  101. </sect3>
  102. <sect3 id="zend.validate.set.email_address.validateidn">
  103. <title>Validating International Domains Names</title>
  104. <para>
  105. <classname>Zend_Validate_EmailAddress</classname> will also match international
  106. characters that exist in some domains. This is known as International Domain Name (IDN)
  107. support. This is enabled by default, though you can disable this by changing the
  108. setting via the internal <classname>Zend_Validate_Hostname</classname> object that
  109. exists within <classname>Zend_Validate_EmailAddress</classname>.
  110. </para>
  111. <programlisting language="php"><![CDATA[
  112. $validator->hostnameValidator->setValidateIdn(false);
  113. ]]></programlisting>
  114. <para>
  115. More information on the usage of <methodname>setValidateIdn()</methodname> appears in
  116. the <classname>Zend_Validate_Hostname</classname> documentation.
  117. </para>
  118. <para>
  119. Please note IDNs are only validated if you allow DNS hostnames to be validated.
  120. </para>
  121. </sect3>
  122. <sect3 id="zend.validate.set.email_address.validatetld">
  123. <title>Validating Top Level Domains</title>
  124. <para>
  125. By default a hostname will be checked against a list of known TLDs. This is enabled by
  126. default, though you can disable this by changing the setting via the internal
  127. <classname>Zend_Validate_Hostname</classname> object that exists within
  128. <classname>Zend_Validate_EmailAddress</classname>.
  129. </para>
  130. <programlisting language="php"><![CDATA[
  131. $validator->hostnameValidator->setValidateTld(false);
  132. ]]></programlisting>
  133. <para>
  134. More information on the usage of <methodname>setValidateTld()</methodname> appears in
  135. the <classname>Zend_Validate_Hostname</classname> documentation.
  136. </para>
  137. <para>
  138. Please note TLDs are only validated if you allow DNS hostnames to be validated.
  139. </para>
  140. </sect3>
  141. <sect3 id="zend.validate.set.email_address.setmessage">
  142. <title>Setting messages</title>
  143. <para>
  144. <classname>Zend_Validate_EmailAddress</classname> makes also use of
  145. <classname>Zend_Validate_Hostname</classname> to check the hostname part of a given
  146. email address. As with Zend Framework 1.10 you can simply set messages for
  147. <classname>Zend_Validate_Hostname</classname> from within
  148. <classname>Zend_Validate_EmailAddress</classname>.
  149. </para>
  150. <programlisting language="php"><![CDATA[
  151. $validator = new Zend_Validate_EmailAddress();
  152. $validator->setMessages(array(Zend_Validate_Hostname::UNKNOWN_TLD => 'I don't know the TLD you gave'));
  153. ]]></programlisting>
  154. <para>
  155. Before Zend Framework 1.10 you had to attach the messages to your own
  156. <classname>Zend_Validate_Hostname</classname>, and then set this validator within
  157. <classname>Zend_Validate_EmailAddress</classname> to get your own messages returned.
  158. </para>
  159. </sect3>
  160. </sect2>
  161. <!--
  162. vim:se ts=4 sw=4 et:
  163. -->