-1

I'm using email validation of zend framework and when I give email address as abcde@gester.tech and it responded with invalid validation. Then I modifed the validation as below.

$emailValidator= new Validator\EmailAddress(Validator\Hostname::ALLOW_DNS | Validator\Hostname::ALLOW_LOCAL);
        $emailRegex= new Validator\Regex(
            array(
                'pattern' => '/^(?:(?:[^<>()\[\]\\.,;:\s@"]+(?:\.[^<>()\[\]\\.,;:\s@"]+)*)|(?:".+"))@(?:(?:\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(?:(?:[a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/',
                'messages'=>array(
                    'regexNotMatch'=>'Make sure your email pattern is correct'
                )
            )
        );
        $emailInp->getValidatorChain()->addValidator($emailValidator)->addValidator($emailRegex);

Now am able to pass the email address (abcde@gester.tech) with out validation error. But if I give the input as abcde@gester it also take as valid input. But I want to restrict that and I think this can be implemented by adding regex to this validation. May I know how to implement that.

 $emailValidator= new Validator\EmailAddress(
            Validator\Hostname::ALLOW_DNS |
            Validator\Hostname::ALLOW_LOCAL);
        $emailRegex= Validator\Regex(array('pattern' => '/^(?:(?:[^<>()\[\]\\.,;:\s@"]+(?:\.[^<>()\[\]\\.,;:\s@"]+)*)|(?:".+"))@(?:(?:\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(?:(?:[a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/'));
        $emailInp->getValidatorChain()->addValidator($emailValidator->addValidator($emailRegex));
  • I edited the answer. Please check. Also, please drop the comment if you need something else. –  Jun 28 '20 at 15:37
  • 1
    The best way to validate an email address is to send an email and check the return value. Please, have a look at these sites: [TLD list](https://www.iana.org/domains/root/db); [valid/invalid addresses](https://en.wikipedia.org/wiki/Email_address#Examples); [regex for RFC822 email address](http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html) – Toto Jun 29 '20 at 07:56
  • Wonderful links @Toto. OP can use these links in order to match email but IMHO; I think the OP needs a way or syntax or construct to implement regex in Zend framework. –  Jun 29 '20 at 08:22
  • Please verify the modified code –  Jun 29 '20 at 11:03

1 Answers1

0

There is a Regex Validator support in Zend framework which facilitates matching the user-defined regex pattern. There is a pattern option which sets the regular expression pattern for the given validator.

You can use it to match the user-defined validator i.e. the pattern that suits your need.

SYNTAX:

$validator = new Zend\Validator\Regex(array('pattern' => '/your_desired_regex_pattern_here/'));

For matching the valid email address that suits your need you can fill in the pattern with available regex validators for email addresses. Also as per the Zend docs; the regex validator uses PCRE(php) syntax, so you can use PCRE regex flavor.

A sample example:

$validator = new Zend\Validator\Regex(array('pattern' => '/^(?:(?:[^<>()\[\]\\.,;:\s@"]+(?:\.[^<>()\[\]\\.,;:\s@"]+)*)|(?:".+"))@(?:(?:\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(?:(?:[a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/'));

$validator->isValid("abcde@gester.tech"); // returns true
$validator->isValid("abcde@gester"); // returns false
$validator->isValid("Someemail@someDomain.com"); // returns true

If you want to chain multiple validators on a single input data you can also use the below syntax:

$validatorChain = new Zend_Validate();
$validatorChain->addValidator(
                    new Zend\Validator\Regex(array('pattern' => '/^(?:(?:[^<>()\[\]\\.,;:\s@"]+(?:\.[^<>()\[\]\\.,;:\s@"]+)*)|(?:".+"))@(?:(?:\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(?:(?:[a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/'))
               ->addValidator(new Zend_Validate_Alnum()); // Second validator...You can chain multiple

if ($validatorChain->isValid($input_email)) {
    // email passed validation
} else {
    // email failed validation; print reasons
    foreach ($validatorChain->getMessages() as $message) {
        echo "$message\n";
    }
}

You can find the demo of the above regex in here.

Reference: The regex for validating email address is taken from this answer.

  • Is it possible to add like this $emailInp->getValidatorChain()->addValidator($emailValidator,$RegexValidator); –  Jun 28 '20 at 15:02
  • Why do you want to chain them? You want to validate the email only isn't it?Please correct me if I'm wrong in understanding your requirement. And also I think if you'll use both it will be redundant. –  Jun 28 '20 at 15:04
  • It also contain some other validation rule and need to validate that also –  Jun 28 '20 at 15:08
  • yeah need to use both –  Jun 28 '20 at 15:11
  • Could you please verify the modifed question –  Jun 29 '20 at 06:55
  • @Ark The correct one should be `$emailValidator= new Validator\EmailAddress( Validator\Hostname::ALLOW_DNS | Validator\Hostname::ALLOW_LOCAL); $emailRegex= new Validator\Regex(array('pattern' => '/^(?:(?:[^<>()\[\]\\.,;:\s@"]+(?:\.[^<>()\[\]\\.,;:\s@"]+)*)|(?:".+"))@(?:(?:\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(?:(?:[a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/')); $emailInp->getValidatorChain()->addValidator($emailValidator)->addValidator($emailRegex);` –  Jun 29 '20 at 08:15
  • How to add custom validation message to this. Now it is showing as `The input does not match against pattern '/^(?:(?:[^<>()\[\]\.,;:\s@"]+(?:\.[^<>()\[\]\.,;:\s@"]+)*)|(?:".+"))@(?:(?:\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(?:(?:[a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/' ` –  Jun 29 '20 at 10:26
  • Need to specify the message only for this regex –  Jun 29 '20 at 10:50
  • Does [**this**](https://framework.zend.com/manual/2.1/en/modules/zend.validator.messages.html) help?Because for complete chain it is possible to get the message not for single validator. Although if you want to display message for single chain; then you need to read the docs. I haven't used it like that before. Because; if I am using the chain then I will display the error message for the complete chain –  Jun 29 '20 at 10:54