How to print error message under respective input field using javascript validation in php
<p>How to print error message under respective input field if left empty and error message  must be removed when filled, how to proceed further i have not used javascript for validation earlier.</p>

<p>script code</p>

<pre><code>function validateForm() {
    var a = document.forms["student_reg"]["name"].value;
    if (a == null || a == "") {
        alert("Name must be filled out");
        return false;
    }
     var b = document.forms["student_reg"]["email"].value;
     if (b == null || b == "") {
        alert("Email must be filled out");
        return false;
    }
     var c = document.forms["student_reg"]["username"].value;
     if (c == null || c == "") {
        alert("Username must be filled out");
        return false;
    }
     var d = document.forms["student_reg"]["password"].value;
     if (d == null || d == "") {
        alert("Password must be filled out");
        return false;
    }
     var e = document.forms["student_reg"]["roll_no"].value;
     if (e == null || e == "") {
        alert("Roll no must be filled out");
        return false;
    }
}
</code></pre>

<p>html code is here</p>

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

 &lt;a href="login.php"&gt;Login&lt;/a&gt;
        &lt;form name="student_reg" method="POST" action="" onsubmit="return validateForm()"&gt;
                &lt;p&gt;NAME:&lt;/p&gt;
                &lt;input type="text" name="name" value="" &gt;
                &lt;span class="error"&gt;&lt;p id="name_error"&gt;&lt;/p&gt;&lt;/span

                &lt;p&gt;EMAIL:&lt;/p&gt;
                &lt;input type="text" name="email" value="" &gt;
                &lt;span class="error"&gt;&lt;p id="email_error"&gt;&lt;/p&gt;&lt;/span

                &lt;p&gt;USERNAME:&lt;/p&gt;
                &lt;input type="text" name="username" value="" &gt;
                &lt;span class="error"&gt;&lt;p id="username_error"&gt;&lt;/p&gt;&lt;/span

                &lt;p&gt;PASSWORD:&lt;/p&gt;
                &lt;input type="password" name="password" value="" &gt;
                &lt;span class="error"&gt;&lt;p id="password_error"&gt;&lt;/p&gt;&lt;/span

                &lt;p&gt;ROLL NO:&lt;/p&gt;
                &lt;input type="number" name="roll_no" value="" &gt;
                &lt;span class="error"&gt;&lt;p id="roll_no_error"&gt;&lt;/p&gt;&lt;/span
                &lt;br/&gt;
                &lt;br/&gt;
                &lt;br/&gt;

                &lt;input type="submit" name="submit" value="submit"&gt;

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