我在我的網站上創建了一個「註冊」頁面,但是當「onsubmit」事件被觸發時,JavaScript驗證功能似乎沒有調用。我已經閱讀了這個網站上關於類似問題的文章,但是他們的問題似乎是他們忘記了返回一個布爾值,指示繼續並執行操作。請注意,此頁面是「包含」在另一個頁面的PHP功能「包含」。表單不會觸發javascript onsubmit事件
<script type="text/javascript">
function validateForm() {
//alert();
var display = document.getElementById('error');
var usernameValue = document.getElementById('username').value;
var passwordValue = document.getElementById('password').value;
var rptPasswordValue = document.getElementById('rptPassword').value;
var emailValue = document.getElementById('email').value;
var aliasValue = document.getElementById('alias').value;
if (usernameValue != '' && passwordValue != '' && rptPasswordValue != '' && emailValue != '' && aliasValue != '') {
if (passwordValue == rptPasswordValue) {
if (usernameValue.length > 32 || aliasValue.length > 32) {
displayError("Username or password is too long (Both fields must contain 32 characters or less)");
} else {
if (passwordValue.length > 32 || passwordValue.length < 4) {
displayError("Password must be between 4 and 32 characters in length.");
} else {
if (emailValue.indexOf('@') == -1 || emailValue.length > 64 || emailValue.length < 6) {
displayError("Please make sure you have entered a valid email. (Emails must be between 6 and 64 characters in length)");
} else {
if (aliasValue < 3 || aliasValue > 32) {
displayError("Your alias must be between 3 and 32 characters in length.");
} else {
return true;
}
}
}
}
} else {
displayError("Please make sure both passwords match.");
}
} else {
displayError("Please make sure each field contains a value.");
}
return false;
}
function displayError(var msg) {
document.getElementById('error').innerHTML = msg;
}
</script>
<h1>Sign Up</h1>
<p>All fields must be filled out in order for an account to be created.</p>
<form onsubmit="return validateForm();" action="register.php" method="post">
<table>
<tr>
<td>
<p class="txtLabel">Username:</p>
</td>
<td>
<input type="text" class="defaultTxt" tabindex="0" id="username" name='username'>
</td>
<td>
<p class="txtLabel">Email Address:</p>
</td>
<td>
<input type="text" class="defaultTxt" tabindex="1" id="email" name='email'>
</td>
</tr>
<tr>
<td>
<p class="txtLabel">Password:</p>
</td>
<td>
<input type="password" class="defaultTxt" tabindex="2" id="password" name='password'>
</td>
</tr>
<tr>
<td>
<p class="txtLabel">Repeat Password:</p>
</td>
<td>
<input type="password" class="defaultTxt" tabindex="2" id="rptPassword" name='rptPassword'>
</td>
</tr>
<tr>
<td>
<p class="txtLabel">Nickname/Alias:</p>
</td>
<td>
<input type="text" class="defaultTxt" tabindex="4" id="alias" name='alias'>
</td>
</tr>
</table>
<p><b id='error' style='color: red'></b></p><br />
<input type="submit" value='' name='submit' class="submitBtn">
</form>
請注意,在validateForm函數中,警報用於測試函數是否被調用,但不是。
任何想法?
謝謝,我不能相信我錯過了這麼明顯的東西:D – Darestium 2012-03-27 04:40:29
@Darestium,沒問題。樂於幫助.. – Milap 2012-03-27 04:54:31