HELP in validating form

Discussion in 'JavaScript' started by mirrior, Jul 23, 2008.

  1. #1
    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);">
                &nbsp;&nbsp;Your Name:<input type='text' name='name' id='name'  />
                <br />
                <table>
                <tr>
                <td valign="top">
    			&nbsp;&nbsp;<p>Comment:</p> </td>
                <td >
                &nbsp;<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.
     
    mirrior, Jul 23, 2008 IP
  2. zurpit.com

    zurpit.com Peon

    Messages:
    1,461
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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);">
                &nbsp;&nbsp;Your Name:<input type='text' name='name' id='name'  />
                <br />
                <table>
                <tr>
                <td valign="top">
    			&nbsp;&nbsp;<p>Comment:</p> </td>
                <td >
                &nbsp;<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:
     
    zurpit.com, Jul 23, 2008 IP