我为一个表单编写了验证代码,它是:
function checkWholeForm(theForm) {
// Create validation variables
var valid = true;
var validationMessage = "Please correct the following errors: \r\n";
// Validate name
if (document.getElementById('name').value.length == 0)
{
validationMessage = validationMessage + ' - Name is missing\r\n';
valid = false;
}
//(and there's a few other functions)
// Display alert box with errors if any errors found
if (valid == false)
{
alert(validationMessage);
}
return valid;
}
然后在 HTML 页面中,它是:
<form action="submit.php" method="post" enctype="text/plain" onsubmit="return checkWholeForm(this)">
表中是:
<input type="text" id="name" name="name" size="20" value="" />
但是当我点击提交时,一个空的文本框不会触发警报。我究竟做错了什么?
编辑: http: //jsbin.com/uvepin/2/edit获取完整的 HTML 和 JS。