PHP validation form data
<p>I've 10 fields,when user submits forms,i need to validate the data in form.</p>

<p>Two things,</p>

<ol>
<li><p>To check whether fields is empty.</p></li>
<li><p>Validating data,if present.</p></li>
</ol>

<p>x would allow only spaces,underscore,aplha numeric characters
y will check whether it is an image or not.</p>

<p>I'm using below code,it does not work.It is not validating and printing error.</p>

<p><strong>Update</strong></p>

<pre><code>$validate = array(
    '/^[a-z\d ]{4,20}$/i' =&gt; array("$x" =&gt; "Please enter valid name."), 
    '/^[a-z\d ]{4,20}$/i' =&gt; array("$y" =&gt; "Please enter a real category.")
);

$error = '';
foreach ($validate as $key =&gt; $field)
{
    if (preg_match($key,$field[0]))
    {
       $error.= $field[0];
    }
}

if($error)
{
    echo $error;
    exit;
}
</code></pre>

<p>Thanks in advance!</p>

<p>Let me know,if you some thing is not clear in this post.</p>
