I have come up with a form validation javascript function.But it always return true although there are empty text fields.Why is that?Following is my code.

HTML Form

<form method="post" action="operation.php" name="form1">
            <label>Location Name:</label><input type="text" name="name"/>
            </br>
            </br>
            <label>Latitude:</label><input type="text" name="age"/>
            </br>
            </br>
            <label>Lontitude:</label><input type="text" name="addr"/>
            </br>
            </br>
            <input type="submit" value="Add" name="add" onclick="return validation"/>
 </form>

Javascript function

<script type="text/javascript">
        function validation(){
            var name=document.forms["form1"]["name"].value;
            var age=document.forms["form1"]["age"].value;
            var address=document.forms["form1"]["addr"].value;
            if((name==null) || (age==null) || (address==null)){
                alert("All the fields should be filled!!");
                return false;
            }else{
                alert("Successfully added!!");
                return true;
            }
        }
</script>

Just a few minor changes to get your validation to work...

<form method="post" action="operation.php" name="form1">
   <label>Location Name:</label><input type="text" name="name" />
   <br /><br />
   <label>Latitude:</label><input type="text" name="age" />
   <br /><br />
   <label>Lontitude:</label><input type="text" name="addr" />
   <br /><br /> 
   <input type="submit" value="Add" name="add" onclick="return validation()" />
 </form>


<script>
   function validation(){
       var name=document.forms["form1"]["name"].value;
       var age=document.forms["form1"]["age"].value;
       var address=document.forms["form1"]["addr"].value;
       if((name=="") || (age=="") || (address=="")){
           alert("All the fields should be filled!!");
           return false;
       }
       else
       {
           alert("Successfully added!!");  
           return true;
       }
   }
</script>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.