2013-04-22 78 views
1

我寫的不提交表單,如果表單驗證失敗

<form action='test.php' method='post'> 
............. 
<submit onclick='validate();'></submit> 
</form> 

function valiadte(){ 
    if(true){ 
     var form = getElementByID(........); 
     form.submit(); 
    } 
    else alert('error'); 
} 

但窗臺去,如果不能test.php的。 如何避免它去test.php如果失敗或我可以傳遞一些消息到test.php?

Thx提前。

原始源代碼:

  <form id="tabS" method="post" action="stdRegister.php"> 
        <label>Account</label> 
        <input id="stdAccount" name="account" type="text" value="" class="input-xlarge" onblur="validateAccountAndID(this,document.getElementById('stdAccountHelp'));"> 
        <span class="help-block" id="stdAccountHelp"></span> 
        <label>Password</label> 
        <input id="stdPassword" name="password" type="password" value="" class="input-xlarge" onblur="validatePassword(this,document.getElementById('stdPasswordHelp'));"> 
        <span class="help-block" id="stdPasswordHelp"></span> 
        <label>Name</label> 
        <input id="stdName" name="name" type="text" value="" class="input-xlarge" onblur="validateName(this,document.getElementById('stdNameHelp'));"> 
        <span class="help-block" id="stdNameHelp"></span> 
        <label>IDNumber</label> 
        <input id="stdID" name="idNumber" type="text" value="" class="input-xlarge" onblur="validateAccountAndID(this,document.getElementById('stdIDHelp'));"> 
        <span class="help-block" id="stdIDHelp"></span> 
        <label>Department</label> 
        <select id="stdDepartment" name="department"> 
         <?php 
          while ($row = $result->fetch_assoc()){ 
            echo"<option>"; 
            echo $row['department']; 
            echo"</option>"; 
         } 
         ?> 
        </select> 
        <label>Grade</label> 
        <select id="stdGrade" name="grade"> 
         <option>1</option> 
         <option>2</option> 
         <option>3</option> 
         <option>4</option> 
        </select> 
        <div> 
         <button class="btn btn-primary" onclick="submitStudetForm();">Create Account</button> 
        </div> 
        <span class="help-block" id="stdRegisterHelp"></span> 
       </form> 

function submitStudentForm(){ 
    var form = document.getElementById('tabP'); 
    if(
     validateAccountAndID(document.getElementById('stdAccount'),document.getElementById('stdAccountHelp')) 
     && validatePassword(document.getElementById('stdPassword'),document.getElementById('stdPasswordHelp')) 
     && validateName(document.getElementById('stdName'),document.getElementById('stdNameHelp')) 
     && validateAccountAndID(document.getElementById('stdID'),document.getElementById('stdIDHelp'))){ 
     form.submit(); 
     alert("success"); 
    } 
    else { 
     alert("Something wrong in the form. Please check again!"); 
     return false; 
     //window.location.href='index.php'; 
    } 
} 
+0

你在測試是真的嗎? – auicsc 2013-04-22 20:18:28

+0

表單驗證類似於長度正則表達式 – 2013-04-22 20:20:56

+1

正如僅供參考,因此您不必干預默認提交,您可以使用'onsubmit'函數執行驗證。通過這種方式,您只需返回true;如果所有驗證都通過並且表單將繼續提交(無需您再次獲取表單並手動提交)或「返回false」;在這種情況下,提交將停止並離開你在頁面上。 – War10ck 2013-04-22 20:21:04

回答

2

只要在else的情況下具有功能return false

function validate(){ 
    if(true){ 
     return true; 
    } 
    else { 
     alert('error'); 
     return false; 
    } 
} 

而且你會改變你的HTML:

<input type="submit" value="Submit" onsubmit="validate()"> 
+0

它似乎不適合我,它仍然前進。 – 2013-04-22 20:24:52

+2

你能顯示你的確切代碼嗎?你是否也改變了html?請記住,現在它說'如果(真)',我假設你有一些實際的驗證,而不是「真」,否則它會一直返回true並提交。 – Jonah 2013-04-22 20:26:44

+0

這就是爲什麼我問他他測試的是什麼「真正的」.. – auicsc 2013-04-22 20:27:42

1

使用此停止表單提交:

else { 
    alert('error'); 
    return false; 
} 

順便說一句,你的函數名稱是 「valiadte」,而不是 「驗證」。