<?php
namespace Myfitmix\Customformvalidation\Observer;

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

    public function __construct(

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

        $this->request = $request;
       $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
    ) {
        $custdata = $this->request->getPost();
        $custcustom_project_name = $custdata['custom_project_name'];

        $regexcustcustom_project_name = '/^[@#$%<>"!`_~:;?\/\^\]&\[*}{)(=]*$/';

        if (preg_match($regexcustcustom_project_name, $custcustom_project_name)) {
            $cartUrl = $this->redirect->getRefererUrl();
            $this->_responseFactory->create()->setRedirect($cartUrl)->sendResponse();
            exit;
        }

    }
}
