表单验证转到下一页

时间:2013-12-10 12:47:07

标签: javascript html

我想验证表单字段。当我提交它不会进入下一页(formsubmitted.php)它停留在那里 plzz告诉我怎么能这样做 表单代码和JS代码在下面我只是在名称字段上进行验证以进行测试...

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> 

1 个答案:

答案 0 :(得分:3)

您没有从事件处理函数返回任何内容。

onsubmit="validateForm()"

应该是

onsubmit="return validateForm()"