我正在尝试验证表单中的文本框,但是当单击提交按钮并且所有框都为空时,所有代码都会继续运行,就像所有代码都已正确填写一样,即使它不是。这是我用来验证文本框的代码
function validateAndEmail(){
if(document.channelForm.chanName.value == “”) {
alert(“Please provide your channel name.”);
Return false;
}
else if(document.channelForm.chanURL.value == “”) {
alert(“Please provide your channel URL.”);
Return false;
}
else if(document.channelForm.famName.value == “”) {
alert(“Please provide your Family name.”);
Return false;
}
else if(document.channelForm.firName.value == “”) {
alert(“Please provide your First name.”);
Return false;
}
else if(document.channelForm.email.value == “”) {
alert(“Please provide your valid Email.”);
Return false;
}
else if(document.channelForm.address.value == “”) {
alert(“Please provide your Address.”);
Return false;
}
else {
alert("Everything is ok. Dont panic!")
Return true;
}
}
我已经使用提示“工作”的警告测试了此按钮,因此它肯定与代码相关联,但是当我尝试使用if语句时,即使满足条件,它也不会运行语句。任何有经验的研究员都能给予我的帮助将不胜感激。
答案 0 :(得分:0)
Return
替换为return
,不带大写。有效功能:
function validateAndEmail() {
var form = document.channelForm;
console.log(form);
if (form.chanName.value == "") {
document.write("Please provide your channel name.");
return false;
} else if (form.chanURL.value == "") {
document.write("Please provide your channel URL.");
return false;
} else if (form.famName.value == "") {
document.write("Please provide your Family name.");
return false;
} else if (form.firName.value == "") {
document.write("Please provide your First name.");
return false;
} else if (form.email.value == "") {
document.write("Please provide your valid Email.");
return false;
} else if (form.address.value == "") {
document.write("Please provide your Address.");
return false;
} else {
document.write("Everything is ok. Dont panic!")
return true;
}
}

<form name="channelForm" onsubmit="validateAndEmail()">
<input name="chanName"><br>
<input name="chanURL"><br>
<input name="famName"><br>
<input name="firName"><br>
<input name="email"><br>
<input name="address"><br>
<input type="submit" value="Ok" />
</form>
&#13;