Zeeshan
Zeeshan

Reputation: 45

form validationgoes to next page

I want to validate form fields. When i submit it does not goes to the next page(formsubmitted.php) it stays there plzz tell me how can i do this The form code n JS code is below i just validate on name field for testing...

form.php

<script type="text/javascript">
<!--
function validateForm()
{
    var name=document.getElementsByClassName("name");
   if(name[0].value == "")
   {
     alert( "Please provide your name!" );
     document.myForm.name.focus() ;
     return false;
   }
   else if(!isNaN(name[0].value))
   {
     alert( "Enter only alphabets!" );
     document.myForm.name.focus() ;
     return false;
   }
}
-->
</script>

<form name="myform" onsubmit="validateForm()" action="formsubmitted.php">
     <span class="required" title="Required Field">*</span>
     <span style="font-size:16px; color:#09c;">Name: </span>    
     <input class="name" title="Enter Your Full Name" type="text" name="name" value=""/><br/>
      <span class="required" title="Required Field">*</span>
     <span style="font-size:16px;color:#09c;">Address:</span>
     <input class="address" title="Enter Address" type="text" name="address" value=""/><br/>
     <span class="required" title="Required Field">*</span>
     <span style="font-size:16px;color:#09c;">Contact Number:</span>
     <input class="contactno"  title="Enter Number" type="text" name="contact" value=""/><br/>
     <input class="submit" type="submit" value="Submit"/></span>        
</form> 

Upvotes: 0

Views: 42

Answers (1)

Quentin
Quentin

Reputation: 943207

You aren't returning anything from your event handler function.

onsubmit="validateForm()"

should be

onsubmit="return validateForm()"

Upvotes: 3

Related Questions