2012-08-23 43 views
0

我有一個表單驗證一個字段。警告框出現在一個錯誤和divs改變,但該頁面繼續提交。我需要幫助弄清楚它爲什麼還在繼續。Javascript驗證:網頁仍提交

的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> 
+0

您需要從validateFormOnSubmit()函數返回true/false,但是您要返回一個字符串。 –

+0

@LeeTaylor再看一遍... – epascarello

+0

'reason'正在調用validateName()函數,該函數根據是否有錯誤返回值或不是。然後,if語句正在檢查最後一個函數是否發回任何內容。如果'reason'中有某些內容,if語句應該返回false,否則該語句返回true。我不確定我是否完全明白你在說什麼。你能詳細說明嗎? – Nicole

回答

0

所以我通過jsbeautifier運行的代碼都會在你的代碼的隨機0。也看看你的錯誤消息。你在每一行都複製了相同的錯誤。你沒有開放<p>標籤。

表單提交的原因可能是因爲您的代碼中存在JS錯誤。在您的JavaScript控制檯上啓用調試器。轉到控制檯並輸入

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

錯誤是否顯示在控制檯中?

而當你這樣做時,你會看到一個空錯誤。爲什麼?因爲你有一個名字而不是一個id。

document.getElementById('error').innerHTML <--I am looking for an id 
<td name='error'> <-- I am not an id! 
+0

結束p標籤必須來自我最後一次使用此代碼。我刪除了他們和0,但仍然做同樣的事情。還有其他建議嗎? – Nicole

+0

是的,看看編輯。學習調試。 :) – epascarello

+0

我的錯誤控制檯沒有任何東西,這就是爲什麼我把它放在這裏。感謝您的幫助。 – Nicole

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; 
} 
+0

它仍然在提交時返回false; – Nicole

+0

絕對不要手動執行,但是您應該通過從按鈕的事件參數調用preventDefault()來防止按鈕正常運行。針對多個瀏覽器測試您的代碼,因爲手動方式是高風險。如果可以的話,選擇jQuery驗證(以及你對jQuery的依賴)。 – roland