New to Javascript -- What is wrong in this code

Discussion in 'JavaScript' started by mfp, Mar 2, 2009.

  1. #1
    i am learning javascript. please help me finding problem in this code.

    
    <html>
      <head>
        <title>Test Javascript</title>
        <script type="text/javascript">
    	function validateForm() 
    	{
    			var name = document.testform.name;
    			var hobby = document.testform.hobby;
    			var age = document.testform.age;
    			var comment = document.testform.comment;
    			
    	     if (name.value == "")
    			{
    			window.alert("Enter your name.");
    		    name.focus();
    			return false;
    			}
    
    	     if (hobby.value == "")
    			{
    			window.alert("Enter your hobby.");
    		    hobby.focus();
    			return false;
    			}
    
    	     if (age.value == "")
    			{
    			window.alert("Enter your age.");
    		    age.focus();
    			return false;
    			}
    
    	     if (comment.value == "")
    			{
    			window.alert("Enter your comment.");
    		    comment.focus();
    			return false;
    			}
    		return true;
    	}
        </script>
    </head>
      <body>
          <form name="testform" action="tt.php" method="POST" onsubmit="return validateForm();" >
              Name:  <input type="text" name="name" />
    		  <br />
    		  <br />
    		  Hobby: <input type="text" name="hobby" />
    		  <br />
    		  <br />
    		  Age: <input type="text" name="age" />
    		  <br />
    		  <br />
    		  Comment: <input type="text" name="comment" />	  
               <input type="button" value="Place Order" />
            </form>
          </body>
    </html>
    
    
    
    HTML:
     
    mfp, Mar 2, 2009 IP
  2. riteshmodi

    riteshmodi Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    can you please tell the error you are getting? because code looks ok
     
    riteshmodi, Mar 3, 2009 IP
  3. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #3
    the button is of type="button" when it should be type="submit"
     
    camjohnson95, Mar 3, 2009 IP