How to validate email in Magento2?

In this editorial, we will learn how to validate email in controller in Magento 2.

Some developer suggest zend_validate method to check validation of emal but we shouldn’t use this. If we use zend_validate then it can be cause of coding standard issue.

So I share with you magento way to validate email.

For email validation you can use below code:

namespace Vendor_Name\Module_Name\File_Path;

use Magento\Framework\Validator\EmailAddress as EmailAddressValidator;

class EmailValidator
	 * @var EmailAddressValidator
	private $emailValidator;
	 * @param EmailAddressValidator $emailValidator
	public function __construct(
		EmailAddressValidator $emailValidator
	) {
	$this->emailValidator = $emailValidator;

	 * Execute method
	public function execute()
		$email = "";
		if(!$this->emailValidator->isValid($email)) {
		    throw new Exception\LocalizedException(new \Magento\Framework\Phrase('Invalid email format'));
		return $email;

I hope this article is simple to grasp.

If you still have any issue feel free to ask and let me know your views to make the better. Share this solution with your other Magento buddies via social media.

Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *