How do i validate a company email using php preg_match function

22 Views Asked by At

I am trying to use the preg_match function in php to validate a company email. I have done some general email validation using the same function before but in this case i want the pattern to suit this company email format below something**@brainstake.tech**

Below is my code snippet, kindly Help me correct the pattern so that it suits my company email format.

private function validateEmail(){  
        $result = null;  
        //$pattern1 = "/^[_a-z0-9-+]+(\.[_a-z0-9-+]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/i";//for all email types  
        $pattern = "/^[_a-z0-9-+]+(\.[_a-z0-9-+]+)*@[brainstake-]*(\.[tech]{2,})$/i";   //for brainstake emails  
        if (!preg_match($pattern, $this->email)){  
            $result = false;  
            echo "Only Brainstake company emails are allowed.";  
        }  
        else{  
            $result = true;  
        }  
        return $result;  
    }  

if ($this->validateEmail() == false){  
    echo "Please use correct email";  
    header("location: ../pages/sign-up.php?error=wrongEmailFormat");  
    exit();  
}  

Result: /sign-up.php?error=wrongEmailFormat

0

There are 0 best solutions below