<?php
namespace Myfitmix\Customformvalidation\Observer;

class Createaccount implements \Magento\Framework\Event\ObserverInterface
{
    /**
     * @var \Magento\Framework\App\RequestInterface
     */
    public $request;
    protected $messageManager;
    protected $_responseFactory;
    protected $_url;

    public function __construct(

        \Magento\Framework\App\RequestInterface $request,
        \Magento\Framework\Message\ManagerInterface $messageManager,
        \Magento\Framework\App\ResponseFactory $responseFactory,
        \Magento\Framework\UrlInterface $url,
        \Magento\Framework\App\Response\RedirectInterface $redirect
    ){

        $this->request = $request;
        $this->messageManager = $messageManager;
       $this->_responseFactory = $responseFactory;
       $this->_url = $url;
       $this->redirect = $redirect;
    }

    /**
     * Execute observer
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(
        \Magento\Framework\Event\Observer $observer
    ) {

       $customerAddress = $this->request->getPost();

        $custprefix = $customerAddress["prefix"];
        $custfirstname = $customerAddress["firstname"];
        $custlastname = $customerAddress["lastname"];
        $custtaxvat = $customerAddress['taxvat'];
        $custcustom_company_name = $customerAddress['custom']['custom_company_name'];
        $custcustom_street = $customerAddress['custom']['custom_street'];
        $custcustom_number = $customerAddress['custom']['custom_number'];
        $custcustom_postcode = $customerAddress['custom']['custom_postcode'];
        $custcustom_city = $customerAddress['custom']['custom_city'];
        $custcustom_country = $customerAddress['custom']['custom_country'];
        $custcustom_phone = $customerAddress['custom']['custom_phone'];
            $regexforname = '/^[0-9@#$%<>"!`_~:;?\/\^\]&\[*}{)(+=]*$/';
            $regextaxvat = '/^[@#$%<>"!`_~:;?\/\^\]&\[*}{)(=]*$/';
            $regexcustom_company_name = '/^[@#$%<>"!`_~:;?\/\^\]&\[*}{)(=]*$/';
            $regexcustom_street = '/^[@#$%<>"!`_~:;?\/\^\]&\[*}{)(=]*$/';
            $regexcustom_number = '/^[@#$%<>"!`_~:;?\/\^\]&\[*}{)(=]*$/';
            $regexcustom_postcode = "/^[0-9]+$/";
            $regexcustom_city = '/^[@#$%<>"!`_~:;?\/\^\]&\[*}{)(=]*$/';
            $regexcustom_country = '/^[@#$%<>"!`_~:;?\/\^\]&\[*}{)(=]*$/';
            $regexcustom_phone ="/^[0-9)(+-]+$/";
        // if (($custprefix != null && preg_match($regexforname, $custprefix)) || ($custfirstname != null && preg_match($regexforname, $custfirstname)) || ($custlastname != null && preg_match($regexforname, $custlastname)) || ($custcustom_company_name != null && preg_match($regexcustom_company_name, $custcustom_company_name)) || ($custcustom_street != null && preg_match($regexcustom_street, $custcustom_street)) || ($custcustom_number != null && preg_match($regexcustom_number, $custcustom_number)) || ($custcustom_postcode != null && !preg_match($regexcustom_postcode, $custcustom_postcode)) || ($custcustom_city != null && preg_match($regexcustom_city, $custcustom_city)) || ($custcustom_country != null && preg_match($regexcustom_country, $custcustom_country)) || ($custcustom_phone != null && !preg_match($regexcustom_phone, $custcustom_phone)) || ($custtaxvat != null && preg_match($regextaxvat, $custtaxvat)))
        // {
        //    $cartUrl = $this->redirect->getRefererUrl();
        //    $this->_responseFactory->create()->setRedirect($cartUrl)->sendResponse();
        //    exit;
        // }
    }
}
