PHP form validation for name, email, and Twitter handle Given the following input values from a form, create a method or
  methods to validate the input based on their requirements. Throw an
  exception if any data is invalid.
  
  1.1 name: is required
  1.2 must be first name AND last name
  
  2.1 email is required
  2.2 is a valid email address
  
  3.1 twitter is optional
  3.2 is a Twitter handle
  
  The input data will be in the following format:

$input = [
    'name' =&gt; '...',
    'email' =&gt; '...',
    'twitter' =&gt; '...'
];



This is the code snippet I worked on. How can I improve my code?  I am not allowed to include or use any other files.

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;PHP Form Validation&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;?php

$name = '';
$email = '';
$twitter = '';
$nameError ="";
$emailError ="";
$twitterError ="";

class dataException extends Exception 
{
      public function errorMessage()
      {
        $message = $this-&gt;getMessage();
        return $message;
      }
}

try
{
    if(isset($_POST['submit']))
    {   
        if (empty($_POST["name"]) &amp;&amp; empty($_POST["email"]))
        {
            throw new dataException('Name and Email are required');
        }
        if (empty($_POST["name"])) 
        {
            throw new dataException('Name is required');
        }
        else
        {
            $name = test_input($_POST["name"]);
            // check name only contains letters and whitespace
            if (!preg_match("/^[a-zA-Z ]*$/",$name))
            {
                throw new dataException('Only letters and white space allowed');
            }   
            else 
            {
                if(str_word_count($name)!=2)
                {
                     throw new dataException('FirstName and LastName is required');
                }
            }
        }
        if (empty($_POST["email"])) 
        {
            throw new dataException('Email is required');
        }    
        else
        {
            $email = test_input($_POST["email"]);
            // check if e-mail address syntax is valid or not
            if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) 
            {
                throw new dataException('Email is invalid');
            }
        }
        //checked vs internet.
        $twitter = test_input($_POST['twitter']);
        if (!isset($_POST['twitter']) || $_POST['twitter'] === '') 
        {
            $twitter = '';
        }
        else 
        {
            if (!preg_match('/^(\@)?[A-Za-z0-9_]+$/', $twitter)) 
            {
                throw new dataException('Invalid twitter handle');
            }
        }
    }
}
catch (dataException $e)
{   
    echo 'Caught exception: ',  $e-&gt;getMessage(), "\n";
}

function test_input($data) 
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}
//php code ends here
?&gt;

&lt;form action="" method="post"&gt;
Name (FirstName and LastName):
&lt;input type="text" name="name" value="&lt;?php echo htmlspecialchars($name);?&gt;"&gt;
* &lt;?php echo $nameError;?&gt;
&lt;br&gt;
&lt;br&gt;
E-mail:
&lt;input type="text" name="email" value="&lt;?php echo htmlspecialchars($email);?&gt;"&gt;
* &lt;?php echo $emailError;?&gt;
&lt;br&gt;
&lt;br&gt;
Twitter:
&lt;input type="text" name="twitter" value="&lt;?php echo htmlspecialchars($twitter);?&gt;"&gt;
 &lt;?php echo $twitterError;?&gt;
 &lt;br&gt;
 &lt;br&gt;
&lt;input type="submit" name="submit" type="submit" value="Submit"&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
Here are just few things I noticed.

Class names
I always try and use uppercase first letters, so DataException rather than dataException.

Exception
The errorMessage() method is redundant, as it just does the same as getMessage(), and it's also not used anywhere.

Function name
test_input doesn't really describe what the function does, as it doesn't actually test, so I renamed it clean_input, which better describes it.

Possible bug
The line if(str_word_count($name) != 2){ would give the error message FirstName and LastName is required if the user enter Homer J Simpson, so I change to see if the count is lower than 2, anything else is fine.

Unused variables
The $*Error variables are echoed, but never have a value assigned to them other than the empty string, so removed references to those.

Checking for data
This maybe more personal preference than anything, but when checking for POST data, I use if(!empty($_POST)).

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;PHP Form Validation&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;?php
    $name            = '';
    $email           = '';
    $twitter         = '';

    class DataException extends Exception {}

    try {
        if(!empty($_POST)){
            if(empty($_POST["name"]) &amp;&amp; empty($_POST["email"])){
                throw new DataException('Name and Email are required');
            }
            if(empty($_POST["name"])){
                throw new DataException('Name is required');
            } else {
                $name = clean_input($_POST["name"]);
                // check name only contains letters and whitespace
                if(!preg_match("/^[a-zA-Z ]*$/", $name)){
                    throw new DataException('Only letters and white space allowed');
                } else {
                    if(str_word_count($name) &lt; 2){
                        throw new DataException('FirstName and LastName is required');
                    }
                }
            }
            if(empty($_POST["email"])){
                throw new DataException('Email is required');
            } else {
                $email = clean_input($_POST["email"]);
                // check if e-mail address syntax is valid or not
                if(!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)){
                    throw new DataException('Email is invalid');
                }
            }
            //checked vs internet.
            $twitter = clean_input($_POST['twitter']);
            if(!isset($_POST['twitter']) || $_POST['twitter'] === ''){
                $twitter = '';
            } else {
                if(!preg_match('/^(\@)?[A-Za-z0-9_]+$/', $twitter)){
                    throw new DataException('Invalid twitter handle');
                }
            }
        }
    } catch(DataException $e){
        echo 'Caught exception: ', $e-&gt;getMessage(), "\n";
    }

    function clean_input($data){
        $data    = trim($data);
        $data    = stripslashes($data);
        $data    = htmlspecialchars($data);
        return $data;
    }


    //php code ends here
    ?&gt;

    &lt;form action="" method="post"&gt;
        Name (FirstName and LastName):
        &lt;input type="text" name="name" value="&lt;?php echo htmlspecialchars($name); ?&gt;"&gt;
        *
        &lt;br&gt;
        &lt;br&gt;
        E-mail:
        &lt;input type="text" name="email" value="&lt;?php echo htmlspecialchars($email); ?&gt;"&gt;    
        &lt;br&gt;
        &lt;br&gt;
        Twitter:
        &lt;input type="text" name="twitter" value="&lt;?php echo htmlspecialchars($twitter); ?&gt;"&gt;
        &lt;br&gt;
        &lt;br&gt;
        &lt;input type="submit" name="submit" type="submit" value="Submit"&gt;
    &lt;/form&gt;
&lt;/div&gt;