weedo tech blog

le blog technique de la team weedo

  • Home
  • Ka on twitter
    • Edit
    • Delete
    • Tags
    • Autopost

    A simpler email address validator for Zend

    I wrote a simpler email validator for Zend. Here it is :

    namespace Weedo\Validate;
    
    /**
     * Weedo EmailAddress Validator class.
     *
     * @author http://twitter.com/kaweedo
     */
    
    class EmailAddress extends \Zend_Validate_Abstract
    {
        const INVALID = 'emailAddressInvalid';
    
        protected $_messageTemplates = array(
            self::INVALID => "Email is not valid"
        );
    
         /**
         * isValid function.
         * Defined by Zend_Validate_Interface
         *
         * Returns true if and only if $value is a valid email address
         * according to RFC2822
         *
         * @access public
         * @link   http://php.net/manual/en/function.filter-var.php
         * @param  string $value
         * @return boolean
         */
    
        public function isValid($value)
        {
            if(filter_var($value, FILTER_VALIDATE_EMAIL) === false) {
                $this->_error(self::INVALID);
                return false;
            } else {
                // We only check the presence of a dot on the domain part
                $components = explode("@", $value);
                $domain = $components[1];
    
                if (strpos($domain, ".") === false) {
                    $this->_error(self::INVALID);
                    return false;
                }
    
                return true;
            }
        }
    }

    Usage example :

    $emailAddressValidator = new Weedo\Validate\EmailAddress();
    /* $emailAddressValidator = new Zend_Validate_EmailAddress(); too complicated */
    
    $this->addElement(
            'text',
            'email',
            array(
                'placeholder'     => ucfirst($this->getTranslator()->translate('Your email'))."*",
                'required'  => true,
                'validators' => 
                array(
                    array('NotEmpty', true, array('messages' => $this->getTranslator()->translate('Email is mandatory'))),
                    array($emailAddressValidator,true),
                ),
            )
        );

    source : http://www.electrictoolbox.com/php-email-validation-filter-var/
    http://php.net/manual/en/function.filter-var.php

    • 9 June 2011
    • Views
    • 0 Comments
    • Permalink
    • Favorited 0 Times
    • Tweet
    • Tweet

    Comments 0 Comments

    Leave a Comment

  • About Karim Cassam Chenaï

    My Google Profile

  • Contributors

    • Ballut Vincent
    • fix
    • Karim Cassam Chenaï
  • Subscribe

    Subscribe to this posterous
    Unsubscribe
    Follow this posterous RSS
    You're a contributor here (Edit)
    This is your Space (Edit)
    Follow by email »
    Get the latest updates in your email box automatically.
  • Follow Me

      Delicious

Theme created for Posterous by Obox