0

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>

Edited by leap_coder: edit

2
Contributors
1
Reply
10
Views
3 Years
Discussion Span
Last Post by JorgeM
0

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>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.