我需要您的助手并帮助修改以下代码,以便在不一起满足的情况下显示验证消息。我的代码是:
function validateForm()
{
var x = "";
var y = "";
if ( (RFC.C1.checked) && (!RFC.Language_e.checked || !RFC.Language_e.checked ))
{
y += "Please Select the Language";
document.getElementById('error').innerHTML = y;
return false;
}
else if (document.RFC.Reason.value=="")
{
x += "Please enter a Reason";
document.getElementById('error').innerHTML = x;
return false;
}
else
{
return true;
}
}
此外,验证消息会显示一次,如果用户没有更正并提交页面,则不会再显示错误。你能帮忙解决这两个问题吗?
答案 0 :(得分:0)
我会做这样的事情:
function validateForm() {
var error = "";
var valid = true;
if ( (RFC.C1.checked) && (!RFC.Language_e.checked || !RFC.Language_e.checked )) {
error += "<li><a href=\"#lang\">Please Select the Language</a></li>";
valid = false;
}
else if (document.RFC.Reason.value=="") {
error += "<li><a href=\"reason\">Please enter a Reason</a></li>";
valid = false;
}
if(valid === false) {
document.getElementById('error').innerHTML = "<ul>" + error + "</ul>";
}
return valid;
}
答案 1 :(得分:0)
function validateForm()
{
var x = "";
var y = "";
var z = "";
if ( (RFC.C1.checked) && (!RFC.Language_e.checked || !RFC.Language_e.checked) )
{
y += "Please Select the Language";
document.getElementById('error').innerHTML = y;
return false;
}
else if ( document.RFC.Reason.value=="" )
{
x += "Please enter a Reason";
document.getElementById('error').innerHTML = x;
return false;
}
else if ( ((RFC.C1.checked) && (!RFC.Language_e.checked || !RFC.Language_e.checked )) && (document.RFC.Reason.value=="") )
{
z += "Please enter a Reason and Language";
document.getElementById('error').innerHTML = z;
return false;
}
else
{
return true;
}
}
顺便说一下你的代码工作但不好。您应该阅读有关编程风格的内容。为了获得良好的验证,您可以使用jQuery Validate插件。