How to add validation in php at proper place?
<p>I am designing a registration form in php. i want to add some validation in my form. validations are related to require field validation. this is my code.</p>

<pre><code>&lt;?php
   include('connect.php');
    if(isset($_REQUEST['Register']))
    {

         $error='';//initialize $error to blank
  if(trim($_POST[userid])==''){
      $error.="Please enter a username!&lt;br /&gt;"; //concatenate the $error Message with a line break 
  }
  if(trim($_POST[password])==''){
      $error.="Please Enter password! &lt;br /&gt;";//concatenate more to $error  
  } 
  if(trim($_POST[email])=='')
  {
    $error.="Please Enter email address !&lt;br /&gt;"; 
  }
  else
   {
        if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST[email])) { 
        $error="The e-mail you entered was not in the proper format!"; 

        }
    }
    if(trim($_POST[regAs])=='NULL')
  {
    $error.="Please select Register As !&lt;br /&gt;"; 
  }

  if($error!='')
  {
    echo "&lt;span style=color:red&gt;$error&lt;/span&gt;";
    //Hmmmm no text is in $error so do something else, the page has verified and the email was valid
    // so uncomment the line below to send the user to your own success page or wherever (swap  
     //yourpage.php with your files location). 
     //echo "script type=\"text/javascript\"&gt;window.location=\yourpage.php\"&lt;script&gt;";  
   }
   else
   {
           $userid=$_REQUEST['userid'];
        $name=$_REQUEST['name'];
       $password=$_REQUEST['password'];
       $repassword=$_REQUEST['repassword'];
       $email=$_REQUEST['email'];
       $regAs=$_REQUEST['regAs'];
       if($password!=$repassword)
        {
           echo "&lt;script&gt;alert('both are diferent password')&lt;/script&gt;";
        }
       else
        {

         mysql_query("insert into login(userid,name,password,email,regAs)  values('$userid','$name','$password','$email','$regAs')");
        }   
   }                










    }
?&gt;
&lt;html&gt;
&lt;head&gt;&lt;/head&gt;
&lt;body&gt;
&lt;form action="" method="post" name="f"&gt;
&lt;table align="center"&gt;
&lt;tr&gt;
&lt;td&gt;User ID :&lt;/td&gt;&lt;td&gt;
&lt;input type="text" name="userid" value=""&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Name :&lt;/td&gt;&lt;td&gt;
&lt;input type="text" name="name" value=""&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Password : &lt;/td&gt;
&lt;td&gt;&lt;input type="password" name="password" value=""&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Re-Enter Password : &lt;/td&gt;
&lt;td&gt;&lt;input type="password" name="repassword" value=""&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Email id :&lt;/td&gt;&lt;td&gt;
&lt;input type="text" name="email" value=""&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Register As :&lt;/td&gt;&lt;td&gt;
&lt;select id="regAs" name="regAs"&gt;
&lt;option value="NULL"&gt;SELECT&lt;/option&gt;
&lt;option value="Artist"&gt;Artist&lt;/option&gt;
&lt;option value="Buyer"&gt;Buyer&lt;/option&gt;
&lt;/select&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type="submit" name="Register" value="Register"&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>and this is the out put screen  to check validation. these error messages are coming at first line. But i want that error must come infrontof each line. for example if user does not enter email then error must show in front of email text box. please suggest me what should i do..?</p>
