2013-12-18 65 views
0

考慮到此問題,我已閱讀了許多帖子,並且出於某種原因,沒有任何解決方案適用於我。我有幾個用於驗證的js函數,這些函數在指定給表單中的onSubmit標記的主函數中調用。這裏是JS代碼:即使js函數返回false,onsubmit仍會傳遞

<script type="text/javascript"> 
function validacijaForme() { 

var username = document.getElementById('usname'); 
var email = document.getElementById('email'); 
var pass1 = document.getElementById('pass1'); 
var pass2 = document.getElementById('pass2'); 


if(validacijaUsername(username) && validacijaEmail(email) && validacijaSifri(pass1,pass2)) 
{ 
    alert('success'); 
    return true; 
} 

return false; 
} 

</script> 

<script type="text/javascript"> 
function validacijaSifri(fieldId1, fieldId2) 
{ 
var two = document.getElementById(fieldId1).value; 
var three = document.getElementById(fieldId2).value; 
if(two == three) { return true; } 
alert("Warning!! passcodes must match!!!"); 
return false; 
} 
</script> 

<script type="text/javascript"> 
function validacijaUsername(usname) 
{ 
var letters = /^[A-Za-z]+$/; 
if(letters.test(usname.value)) 
{ 
    return true; 
} 
else 
{ 
    alert('Username must have alphabet characters only'); 
    document.getElementById('usrname').focus(); 
    return false; 
} 
} 
</script> 

<script type="text/javascript"> 
function validacijaEmail(email) 
{ 
var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/; 
if(mailformat.test(email)) 
{ 
    return true; 
} 
else 
{ 
    alert("You have entered an invalid email address!"); 
    document.getElementById(email).focus(); 
    return false; 
} 

} 

這裏是HTML表單代碼

<form class="form-signin" onSubmit="return validacijaForme();" name="registerForm" method = "POST" action="test.php" enctype="multipart/form-data"> 
<h2 class="form-signin-heading">Registracija</h2> 
<input type="text" class="form-control" placeholder="Username" name="usrname" id="usname" autofocus> 
<input type="text" class="form-control" placeholder="E-mail" name="email" id="email"> 
<input type="password" class="form-control" placeholder="Password" name="pass" id="pass1"> 
<input type="password" class="form-control" placeholder="Confirm password" id="pass2"> 
<button class="btn btn-lg btn-primary btn-block" name="dugme" type="submit">Register</button> 

的問題是,所有的警報工作,這意味着這些功能做驗證字段,並應返回false,但onSubmit標記似乎不接受該函數的值。 形式不提交和未通過只test.php的,如果我輸入:

<form class="form-signin" onSubmit="return false" name="registerForm" method = "POST" action="test.php" enctype="multipart/form-data"> 
+0

你能給我們聯繫呢? – Shadow

+0

對不起,不明白,鏈接是什麼? – Matija

+0

您的功能有很多錯誤 – Shadow

回答

0

提交表單使用javascript submit()

<form id="myform" class="form-signin" onSubmit="validacijaForme();" name="registerForm" 
method = "POST" action="test.php" enctype="multipart/form-data"> 

而且在你的函數:

function validacijaForme() { 
var username = document.getElementById('usname'); 
var email = document.getElementById('email'); 
var pass1 = document.getElementById('pass1'); 
var pass2 = document.getElementById('pass2'); 

if(validacijaUsername(username) && validacijaEmail(email) && validacijaSifri(pass1,pass2)) 
{ 
alert('success'); 
document.getElementById("myform").submit(); 
return false; 
} 
return false; 
} 
+0

謝謝,但不幸的是它不起作用 – Matija

+0

你也把ID放在窗體標籤中? – Michel

0

您的問題與您的用戶名/名稱一致性有關。使用控制檯(F12)來調試這種事情。

<input type="text" class="form-control" placeholder="Username" name="usRname" id="usname" autofocus> 

變化:

document.getElementById('usrname').focus(); 

要:

document.getElementById('usname').focus(); 
+0

非常感謝你我完全沒有注意到,這是問題的一部分,另一部分是我正在調用document.getElementById('usname')。focus();並沒有必要,因爲我已經將元素傳遞給該函數,它一定會導致一些異常,從而阻止執行行返回false。 – Matija

+0

是的,一旦打開控制檯就很容易找到。今天將此概念添加到您的調試工具帶中! –

+0

瞭解,會做! – Matija

0

有很多代碼中的好友的錯誤| WORKING DEMO

function validacijaForme() { 

var username = document.getElementById('usname'); 
var email = document.getElementById('email'); 
var pass1 = document.getElementById('pass1'); 
var pass2 = document.getElementById('pass2'); 


if(validacijaUsername(username) && validacijaEmail(email) && validacijaSifri(pass1,pass2)) 
{ 
    alert('success'); 
    return true; 
} 

return false; 
} 

function validacijaSifri(fieldId1, fieldId2) 
{ 
var two = fieldId1.value; 
var three = fieldId2.value; 
if(two == three) { return true; } 
alert("Warning!! passcodes must match!!!"); 
return false; 
} 

function validacijaUsername(usname) 
{ 
    var letters = /^[A-Za-z]+$/; 

    if(letters.test(usname.value)) 
    { 
     return true; 
    } 
    else 
    { 
     alert('Username must have alphabet characters only'); 
     document.getElementById('usname').focus(); 
     return false; 
    } 
} 

function validacijaEmail(email) 
{ 
var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/; 
if(mailformat.test(email.value)) 
{ 
    return true; 
} 
else 
{ 
    alert("You have entered an invalid email address!"); 
    document.getElementById('email').focus(); 
    return false; 
} 

} 
+0

注意到的錯誤,我一直在複製一些代碼,這顯然是一個壞主意,我現在糾正它,謝謝.. – Matija

相關問題