Email.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Email validation rule
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * LICENSE: This source file is subject to version 3.01 of the PHP license
  9. * that is available through the world-wide-web at the following URI:
  10. * http://www.php.net/license/3_01.txt If you did not receive a copy of
  11. * the PHP License and are unable to obtain it through the web, please
  12. * send a note to license@php.net so we can mail you a copy immediately.
  13. *
  14. * @category HTML
  15. * @package HTML_QuickForm
  16. * @author Bertrand Mansion <bmansion@mamasam.com>
  17. * @copyright 2001-2009 The PHP Group
  18. * @license http://www.php.net/license/3_01.txt PHP License 3.01
  19. * @version CVS: $Id: Email.php,v 1.7 2009/04/04 21:34:04 avb Exp $
  20. * @link http://pear.php.net/package/HTML_QuickForm
  21. */
  22. /**
  23. * Email validation rule
  24. *
  25. * @category HTML
  26. * @package HTML_QuickForm
  27. * @author Bertrand Mansion <bmansion@mamasam.com>
  28. * @version Release: 3.2.11
  29. * @since 3.2
  30. */
  31. class HTML_QuickForm_Rule_Email extends HTML_QuickForm_Rule
  32. {
  33. var $regex = '/^((\"[^\"\f\n\r\t\v\b]+\")|([\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+(\.[\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+)*))@((\[(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))\])|(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))|((([A-Za-z0-9\-])+\.)+[A-Za-z\-]+))$/';
  34. /**
  35. * Validates an email address
  36. *
  37. * @param string $email Email address
  38. * @param boolean $checkDomain True if dns check should be performed
  39. * @access public
  40. * @return boolean true if email is valid
  41. */
  42. function validate($email, $checkDomain = false)
  43. {
  44. // Fix for bug #10799: add 'D' modifier to regex
  45. if (preg_match($this->regex . 'D', $email)) {
  46. if ($checkDomain && function_exists('checkdnsrr')) {
  47. $tokens = explode('@', $email);
  48. if (checkdnsrr($tokens[1], 'MX') || checkdnsrr($tokens[1], 'A')) {
  49. return true;
  50. }
  51. return false;
  52. }
  53. return true;
  54. }
  55. return false;
  56. } // end func validate
  57. function getValidationScript($options = null)
  58. {
  59. return array(" var regex = " . $this->regex . ";\n", "{jsVar} != '' && !regex.test({jsVar})");
  60. } // end func getValidationScript
  61. } // end class HTML_QuickForm_Rule_Email
  62. ?>