2012-12-28 435 views
0

我目前正在處理一些非常基本的驗證,用一個簡單的PHP腳本將一堆信息從Web表單發佈到SQL數據庫。 PHP腳本工作正常,並將值發佈到數據庫,但我寫的驗證似乎被忽略。我試圖修復它一段時間,並沒有看到任何明顯的錯誤,但道歉,如果它是微不足道的。JavaScript表單驗證 - 函數未在提交時運行。

HTML:

<form name = "registerForm" method = "POST" onsubmit = "return ValidateRegistration()" action = "createUser.php"> 
    <p class = "register">Desired Username*</p> 
    <p class = "registerDesc">Between 1 and 20 characters long</p>     
    <input type = "text" name = "username" class = "register"> 
    <p class = "register">Desired Password*</p> 
    <p class = "registerDesc">We won't force securty onto you; between 6 and 255 characters long. No other criteria.</p> 
    <input type = "password" name = "password" class = "register"> 
    <p class = "register">Confirm Password*</p> 
    <input type = "password" name = "confirmPassword" class = "register"> 
    <p class = "register">Email Adress*</p> 
    <input type = "text" name = "email" class = "registerEmail"> 
    <p class = "register">Bio</p> 
    <p class = "registerDesc">Extra information such as hobbies, occupation, background information. Maximum 4096 characters. We can do this later.</p> 
    <textarea name = "bio" class = "registerBio"></textarea> 
    <p><input type = "submit" class = "registerButton" value = "Register"></p> 
</form> 

的JavaScript:

function ValidateRegistration() 
{ 
    //username block 
    var username=document.forms["registerForm"]["username"].value; 
    if (username==null || username==" ") 
    { 
     alert("You must provide a username"); 
     return false; 
    }  
    if (username.length>20 || username.length<1) 
    { 
     alert("Sorry, your username must be between 1 and 20 characters long."); 
     return false; 
    } 
    //etc.... 
} 
+1

你永遠不會返回true – HMarioD

+0

@HMarioD謝謝你的回覆,我的印象是不需要返回true,因爲它將是默認值?無論哪種方式添加它不會使該功能的工作。就像我說的,謝謝。 – jdurc

回答

2

究竟你的代碼爲我工作:

創建以下test.html文件放在和Chrome中打開:

<html> 
<head> 
<script> 
function ValidateRegistration() 
{ 
    //username block 
    var username=document.forms["registerForm"]["username"].value; 
    if (username==null || username==" ") 
    { 
     alert("You must provide a username"); 
     return false; 
    }  
    if (username.length>20 || username.length<1) 
    { 
     alert("Sorry, your username must be between 1 and 20 characters long."); 
     return false; 
    } 
    //etc.... 
} 
</script> 
</head> 
<body> 
<form name = "registerForm" method = "POST" onsubmit = "return ValidateRegistration()" action = "createUser.php"> 
    <p class = "register">Desired Username*</p> 
    <p class = "registerDesc">Between 1 and 20 characters long</p>     
    <input type = "text" name = "username" class = "register"> 
    <p class = "register">Desired Password*</p> 
    <p class = "registerDesc">We won't force securty onto you; between 6 and 255 characters long. No other criteria.</p> 
    <input type = "password" name = "password" class = "register"> 
    <p class = "register">Confirm Password*</p> 
    <input type = "password" name = "confirmPassword" class = "register"> 
    <p class = "register">Email Adress*</p> 
    <input type = "text" name = "email" class = "registerEmail"> 
    <p class = "register">Bio</p> 
    <p class = "registerDesc">Extra information such as hobbies, occupation, background information. Maximum 4096 characters. We can do this later.</p> 
    <textarea name = "bio" class = "registerBio"></textarea> 
    <p><input type = "submit" class = "registerButton" value = "Register"></p> 
</form> 
</body> 
</html> 

提交emtpy形式提醒:

對不起,您的用戶名必須介於1到20個字符之間。

+0

你好,我已經給出了一個去,但它仍然做同樣的事情 - 忽略腳本。謝謝你的回答。 – jdurc

+1

如果您使用的是最新的Chrome版本,則應該可以使用! 在Javascript控制檯Ctrl + Shift + J中出現錯誤嗎? – SailAvid

+0

這太奇怪了,也許這是我的XAMPP服務器或其他問題。無論哪種方式,你一定會盡可能地幫助你,如果它對你有用,那麼這個問題本質上是沒有意義的,所以我會接受你的答案。感謝您的幫助。 – jdurc

0

我想擺脫在的onsubmit的回報。

+0

非常感謝您的回覆,不幸的是它仍然無法工作(這也是我的第一個念頭)。 – jdurc

+0

你有一個JS驗證器/探測器嗎?應該有一個內置的,如果不是,你應該能夠爲你的瀏覽器找到一個。在那裏你可以看到它是否真的在運行,如果有的話,可能會有什麼問題。 –

+0

我正在開發最新版本的Chrome,所以我想有一個內置的。我會看看,看看我能找到什麼,再次感謝你。 – jdurc

0

把你的代碼放在try/catch塊中看看發生了什麼。

try{ 
    //your code 
}catch(er){ 
    alert(er); 
} 
1

http://fiddle.jshell.net/hmariod/LRZkR/

<form name = "registerForm" method = "POST" action = "createUser.php"> 
    <p class = "register">Desired Username*</p> 
    <p class = "registerDesc">Between 1 and 20 characters long</p>     
    <input type = "text" name = "username" class = "register"> 
    <p class = "register">Desired Password*</p> 
    <p class = "registerDesc">We won't force securty onto you; between 6 and 255 characters long. No other criteria.</p> 
    <input type = "password" name = "password" class = "register"> 
    <p class = "register">Confirm Password*</p> 
    <input type = "password" name = "confirmPassword" class = "register"> 
    <p class = "register">Email Adress*</p> 
    <input type = "text" name = "email" class = "registerEmail"> 
    <p class = "register">Bio</p> 
    <p class = "registerDesc">Extra information such as hobbies, occupation, background information. Maximum 4096 characters. We can do this later.</p> 
    <textarea name = "bio" class = "registerBio"></textarea> 
    <p><input type = "button" onclick="ValidateRegistration();" class = "registerButton" value = "Register"></p> 
</form>​ 

function ValidateRegistration(){ 
    try{ 
     var username=document.forms["registerForm"]["username"].value; 
     if (username.length < 1) 
     { 
      alert("You must provide a username"); 
      return; 
     }  
     if (username.length>20 || username.length<1) 
     { 
      alert("Sorry, your username must be between 1 and 20 characters long."); 
      return; 
     } 
     document.forms["registerForm"].submit(); 
    }catch(er){ 
     alert(er); 
    } 
}​ 
+0

@jdurk:工作嗎? – HMarioD

0

我認爲這個問題是在比較線

if (username==null || username==" ") 

這應該是

if (username==null || username=="") 

我希望它可以解決你的問題。請讓我知道;-)

+0

你好!感謝您的回覆,但是我擔心這不再相關。我重新輸入了整個代碼,並在發佈後數天內運行。直到今天,我不知道哪裏出了什麼問題,但我願意假設我在某處錯誤地輸入了一些東西。 儘管非常感謝您的回覆。 – jdurc