表单是否在Javascript验证失败后仍在提交?
我这里有一张表格:表单是否在Javascript验证失败后仍在提交?,javascript,forms,validation,submit,Javascript,Forms,Validation,Submit,我这里有一张表格: <span class="aligncenter button"><a href="#" onclick="submitForm('newIncident');">Submit</a></span> 因此,我将每个验证作为一个单独的函数,在提交表单时按顺序调用该函数。如果我注释掉“document.getElementById(myForm.submit();”,验证将按预期运行。但是,如果我不加注释,即使验证失败,它也会每次
<span class="aligncenter button"><a href="#" onclick="submitForm('newIncident');">Submit</a></span>
因此,我将每个验证作为一个单独的函数,在提交表单时按顺序调用该函数。如果我注释掉“document.getElementById(myForm.submit();”,验证将按预期运行。但是,如果我不加注释,即使验证失败,它也会每次提交。我怎样才能阻止这种事情发生
谢谢
编辑:
这是我正在运行的验证之一。它们的结构都是一样的。我应该在某个地方返回布尔值true/false?我该如何在下面的这一页中插入
function validateDisposition()
{
var vIncidentDisposition = document.forms['newIncident']['incidentDisposition'].value;
if (vIncidentDisposition == '')
{
document.forms['newIncident']['incidentDisposition'].className = 'required';
}
else
{
document.forms['newIncident']['incidentDisposition'].className = 'formborder';
}
}
假设您的验证函数返回bool,您应该有
if( validateTime() && validateDate() && validateAge()... etc ) {
if (vDemAge == 'Age' || vDemAge == '') // If Age is not entered, set the value to be blank
{
document.forms['newIncident']['demAge'].value = '';
}
if (vBibNumber == 'Bib #' || vBibNumber == '') // If Bib # is not entered, set the value to blank
{
document.forms['newIncident']['bibNumber'].value = '';
}
document.getElementById(myForm).submit();
}
我让它工作了!布尔思想使我走上了正确的道路。谢谢
我只是在每个验证中添加了“returntrue”和“returnfalse”,然后使用上面的答案和“&&”if将逻辑构建到myform“if”中。如果它没有通过所有的测试,那么其他测试将执行“returnfalse”。工作起来很有魅力 你能展示一些验证功能吗?你在哪里告诉表单不要提交?是的,这是我的怀疑。他们可能认为来自其他验证的“returnfalse”也会阻止更高级别的函数运行,但事实并非如此
if( validateTime() && validateDate() && validateAge()... etc ) {
if (vDemAge == 'Age' || vDemAge == '') // If Age is not entered, set the value to be blank
{
document.forms['newIncident']['demAge'].value = '';
}
if (vBibNumber == 'Bib #' || vBibNumber == '') // If Bib # is not entered, set the value to blank
{
document.forms['newIncident']['bibNumber'].value = '';
}
document.getElementById(myForm).submit();
}