使用onsubmit的表单验证总是返回true

form validation with onsubmit always return true take action

本文关键字:返回 true 验证 onsubmit 表单 使用      更新时间:2023-09-26

我有一个形式,我正试图验证与javascript警报ok!代码为-

    <form class="testimonialForm" id="testimonialForm" name="testimonialForm" 
     method="post" enctype="multipart/form-data" action="addtestimonial.php" 
     onSubmit="return validateForm()">
     <li><label for="name">Name <em>*</em></label>
     <input name="testimonial_submitter_name"  value="{$testimonial_submitter_name}"
    id="testimonial_submitter_name" 
class="required" minlength="2" placeholder="your name here!"/>
            </li>

和我使用的javascript是

function validateForm()
{
var v1=document.getElementById("testimonial_submitter_name").value;
    var v2=document.getElementById("testimonial_title").value;
if(v1=="")
alert ("enter the name");
}

如果提交的是空表单,它会提示在警告框中显示的内容。但是表单被提交了!
有什么问题吗?
如何解决?帮我一把!
提前感谢!

function validateForm() {
    var name = document.getElementById("testimonial_submitter_name").value;
    var title = document.getElementById("testimonial_title").value;
    if (name == "") {
        alert("enter the name");
        return false;
    }
    return true;
}