here is my form and the javascript that runs to check the form if the info. is enter or not <script language="JavaScript" type="text/javascript"> function checkform ( form ) { if (form.elements[0].value == "" || form.elements[0].value==NULL) { alert( "Please enter your name." ); form.elements[0].focus(); return false; } if (form.elements[1].value == "" || form.elements[1].value==NULL) { alert( "Please enter your comment." ); form.elements[1].focus(); return false; } return true; } </script> <form name="myform" method='GET' action='addcomment.php' onsubmit="return checkform(this);"> Your Name:<input type='text' name='name' id='name' /> <br /> <table> <tr> <td valign="top"> <p>Comment:</p> </td> <td > <textarea name='comment' rows='5' id='comment' ></textarea> </td> </tr> <tr> <td> <input type='submit' value='add' /> </td> <td> <input type='reset' /> </td> </tr> <input type='hidden' value='<?php echo $temp?>' name='id'/> </table> </form> Code (markup): And the problem is I cant validate two field(name and comment)at the same time. I mean if I leave "comment" blank, the form would still be submitted. Another issue, if I change the order of the "if" statment above, the form would be submitted if I leave "name" blank. another thing I want to mention that I tried to change the second "if" statment to "else if", but still doesnt work. Any help will be appreciated.
Here, I went through your script and corrected a few minor things. Try this: <script language="JavaScript" type="text/javascript"> function checkform (form) { if (!form.elements[0].value) { alert( "Please enter your name." ); form.elements[0].focus(); return false; } if (!form.elements[1].value) { alert( "Please enter your comment." ); form.elements[1].focus(); return false; } return true; } </script> <form name="myform" method='GET' action='addcomment.php' onsubmit="return checkform(this);"> Your Name:<input type='text' name='name' id='name' /> <br /> <table> <tr> <td valign="top"> <p>Comment:</p> </td> <td > <textarea name='comment' rows='5' id='comment' ></textarea> </td> </tr> <tr> <td> <input type='submit' value='add' /> </td> <td> <input type='reset' /> </td> </tr> <input type='hidden' value='<?php echo $temp?>' name='id'/> </table> </form> PHP: