2014-02-13 78 views
0

我想建立一個註冊腳本,需要所有的表單項目進行驗證或提交必須取消。 我看了很多論壇和許多答案,我看不出我的答案有什麼不同。 HTML表單提交取消JavaScript不工作

<form onSubmit='checkAll()' method='post' action='mydestination.php'> 
<table> 
    <tr> 
    <td><input type='text' name='username' id='txtUsername' class='textboxLargeStyle' placeholder='Username' onChange='checkUsername("false")'/></td> 
    </tr> 
    <tr> 
    <td><input type='text' name='email' id='txtEmail' class='textboxLargeStyle' placeholder='Email Address' onChange='checkEmail()'/></td> 
    </tr> 
    <tr> 
    <td><input type='password' id='pwd1' name='password' class='textboxLargeStyle' placeholder='Password' onkeyup='checkPassword()'/></td> 
    </tr> 
    <tr> 
    <td><input type='password' id='pwd2' name='password2' class='textboxLargeStyle' placeholder='Re-type Password' onkeyup='checkPasswordMatches()'/></td> 
    </tr> 
    <tr> 
    <td><input type='submit' class='buttonLargeStyle' value='Sign Up'/></td> 
    </tr> 
    <tr> 
    <td colspan='2'><small id='Error'>Ensure you have all ticks before submitting! Hover over a cross to see the error.</small></td> 
    </tr> 
</table> 
</form> 

JAVASCRIPT

function checkAll() 
{ 
if(usernameOK == true) 
{ 
    if(emailOK == true) 
    { 
     if(passwordOK == true) 
     { 
      return true; 
     } 
    } 
} 
$('#Error').fadeIn(100); 
return false; 
} 

當我點擊提交和項目得不到滿足,形式仍然提交。

回答

2

你必須從onsubmit處理器

<form onSubmit='return checkAll()' method='post' action='mydestination.php'> 
+0

這是完美的返回由checkAll返回false值。它總是我忘記的小事。非常感謝:D –

0
function checkAll(e) 
{ 
    e.stopPropogation(); //Stops bubbling 
    e.preventDefault(); //prevents anything else that would happen from the click (submit) 

    if(usernameOK && (emailOK && passwordOK) { 
    e.currentTarget.submit();//Submits form 
    } 

    $('#Error').fadeIn(100); 
    return false; 
}