我目前正在處理一些非常基本的驗證,用一個簡單的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....
}
你永遠不會返回true – HMarioD
@HMarioD謝謝你的回覆,我的印象是不需要返回true,因爲它將是默認值?無論哪種方式添加它不會使該功能的工作。就像我說的,謝謝。 – jdurc