Zend framework2 email validation for specific address like for gmail and to block a specific email address

Tag: zend-framework2 Author: hdwja Date: 2013-04-18

I have my input filter for email address and I want to restrict a specific email service provider, please help me out with this.

I have one more form and I want to allow that restricted email service provider only to be registered here. please tell me how can I restrict one particular email service provider and in another form allow only one specific email service provider.

Thanks in advance

$inputFilter->add($factory->createInput(array(
                'name'     => 'email',
                'required' => true,
                'filters'  => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array(
                        'name'    => 'EmailAddress',
                        'options' => array(
                            'messages' => array( 
                                'emailAddressInvalidFormat' => 'Email address format is not invalid', 
                            )
                        ),
                    ),
                    array ( 
                        'name' => 'NotEmpty', 
                        'options' => array( 
                            'messages' => array( 
                                'isEmpty' => 'Email address is required', 
                            ) 
                        ), 
                    ),  
                ),
            )));
I'd probably just use Zend\Validator\Regex. I'm not sure what you mean about two different forms, but you could easily use a the regex validator in both (with two different regexes, of course)

Best Answer

Add a regex validator:

        $inputFilter->add($factory->createInput(array(
            'name'     => 'email',
            'required' => true,
            'filters'  => array(
                array('name' => 'StripTags'),
                array('name' => 'StringTrim'),
            ),
            'validators' => array(
                array(
                    'name'    => 'EmailAddress',
                    'options' => array(
                        'messages' => array(
                            'emailAddressInvalidFormat' => 'Email address format is not invalid',
                        )
                    ),
                ),
                array (
                    'name' => 'NotEmpty',
                    'options' => array(
                        'messages' => array(
                            'isEmpty' => 'Email address is required',
                        )
                    ),
                ),
                array(
                    'name' => 'Regex',
                    'options' => array(
                        'pattern' => '/@gmail.com$/',
                        'messages' => array(
                            'regexNotMatch' => 'You must have a gmail.com email address'
                        ),
                    )
                ),
            ),
        )));