<?php
namespace Myfitmix\Customformvalidation\Observer;

class Accountinformation 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
    ){

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

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

        $custdata = $this->request->getPost();
        $custprefix = $custdata['prefix'];
        $custfirstname = $custdata['firstname'];
        $custlastname = $custdata['lastname'];
        $custtaxvat = isset($custdata['taxvat'])? $custdata['taxvat']:null;
        $custcustom_company_name = isset($custdata['custom']['custom_company_name']) ? $custdata['custom']['custom_company_name'] : null;
        $custcustom_street = isset($custdata['custom']['custom_street'])? $custdata['custom']['custom_street'] : null;
        $custcustom_number = isset($custdata['custom']['custom_number'])? $custdata['custom']['custom_number'] : null;
        $custcustom_postcode = isset($custdata['custom']['custom_postcode'])? $custdata['custom']['custom_postcode'] : null;
        $custcustom_city = isset($custdata['custom']['custom_city'])? $custdata['custom']['custom_city'] : null;
        $custcustom_country = isset($custdata['custom']['custom_country'])? $custdata['custom']['custom_country'] : null;
        $custcustom_phone = isset($custdata['custom']['custom_phone'])? $custdata['custom']['custom_phone'] : null;

        $regexforname = '/^[0-9@#$%<>"!`_~:;?\/\^\]&\[*}{)(+=]*$/';
            $regextaxvat = '/^[@#$%<>"!`_~:;?\/\^\]&\[*}{)(=]*$/';
            $regexcustom_company_name = '/^[@#$%<>"!`_~:;?\/\^\]&\[*}{)(=]*$/';
            $regexcustom_street = '/^[@#$%<>"!`_~:;?\/\^\]&\[*}{)(=]*$/';
            $regexcustom_number = '/^[@#$%<>"!`_~:;?\/\^\]&\[*}{)(=]*$/';
            $regexcustom_postcode = "/^[0-9]+$/";
            $regexcustom_city = '/^[@#$%<>"!`_~:;?\/\^\]&\[*}{)(=]*$/';
            $regexcustom_country = '/^[@#$%<>"!`_~:;?\/\^\]&\[*}{)(=]*$/';
            $regexcustom_phone ="/^[0-9)(+-]+$/";
        if (preg_match($regexforname, $custfirstname) || 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)))
        {
           $cartUrl = $this->_url->getUrl('*/*/edit');
           $this->_responseFactory->create()->setRedirect($cartUrl)->sendResponse();
           exit;
        }
    }
}
