0

我有一个验证一个字段的表单。警告框出现在错误和 div 更改,但页面继续提交。我需要帮助弄清楚为什么它仍在继续。

Javascript:

function validateFormOnSubmit(theForm) {
    var reason = '';

    reason += validateName(theForm.signature_name);

    if (reason != '') {
        document.getElementById('error').innerHTML = "<p>* All fields are required.<br><span style='color:red'><strong><u>Some fields need correction:</u></strong></span></p>" // + reason;    
        //alert("Some fields need correction:\n" + reason);
        return false;
    }
    return true;
}

function validateName(fld) {
    var error = '';
    var illegalChars = /\W\s/; // allow letters, numbers, and underscores

    if (fld.value == '') {
        error = document.getElementById('client_name').innerHTML = "<h2 id='client_name' style='color:red'>Client's Last Name:</h2><br>The required field has not been filled in.";
        alert("Do Not Continue");
    } else if ((fld.value.length < 2) || (fld.value.length > 15)) {
        error = document.getElementById('client_name').innerHTML = "<h2 id='client_name' style='color:red'>Client's Last Name:</h2><br>The name is not long enough.";
        alert("Do Not Continue");
    } else if (illegalChars.test(fld.value)) {
        error = document.getElementById('client_name').innerHTML = "<h2 id='client_name' style='color:red'>Client's Last Name:</h2><br>The name field contains illegal characters.";
        alert("Do Not Continue");
    }
    return error;
}

HTML

<form enctype='multipart/form-data' name='finishJob' id='finishJob' action='finishJob.php' method='POST' onsubmit='return validateFormOnSubmit(this)'>
    <table width='95%' class='tablebox'>
        <tr><td name='error'></td><td><h2>Upload Signature:</h2></br><input name='uploadedfile' id='uploadedfile' type='file' /></td><td><h2 id='client_name'>Client's Last Name:</h2></br><input type='text' id='signature_name' name='signature_name'></td><td><input type='submit' value='Complete Job' class='link-button'></td></tr>
    </table>
</form>
4

2 回答 2

0

如果您在验证后在 javascript 中提交表单怎么办?像这样:

<form enctype='multipart/form-data' name='finishJob' id='finishJob' action='finishJob.php' method='POST' onsubmit='javascript:validateFormOnSubmit(this);return false;'>
    <table width='95%' class='tablebox'>
        <tr><td name='error'></td><td><h2>Upload Signature:</h2></br><input name='uploadedfile' id='uploadedfile' type='file' /></td><td><h2 id='client_name'>Client's Last Name:</h2></br><input type='text' id='signature_name' name='signature_name'></td><td><input type='submit' value='Complete Job' class='link-button'></td></tr>
    </table>
</form>

function validateFormOnSubmit(theForm) {
var reason = '';

  reason += validateName(theForm.signature_name);

  if (reason != '') {
document.getElementById('error').innerHTML="<p>* All fields are required.<br><span style='color:red'><strong><u>Some fields need correction:</u></strong></span></p>"// + reason;   
//alert("Some fields need correction:\n" + reason);
return false;
  } 
  document.finishJob.submit();
  return true; 
}
于 2012-08-23T20:03:05.997 回答
0

所以我通过jsbeautifier运行代码它在你的代码中显示一个随机0的。另请查看您的错误消息。您在每一行都复制了相同的错误。您没有开始<p>标签。

表单提交的原因可能是代码中某处的 JS 错误。在 JavaScript 控制台上启用调试器。转到控制台并输入

validateFormOnSubmit(document.getElementById("finishJob"));

控制台中是否出现错误?

当你这样做时,你会看到一个空错误。为什么?因为你有名字而不是身份证。

document.getElementById('error').innerHTML <--I am looking for an id
<td name='error'>  <-- I am not an id!
于 2012-08-23T20:09:10.773 回答