我创建了一个"注册"在我的网站上的页面,但是当" onsubmit"时,javascript验证功能似乎没有调用。事件被解雇了。我已经阅读过本网站关于类似问题的帖子,但他们的问题似乎是他们忘记返回一个布尔值,表示继续并执行操作。请注意,此页面包含"包含"在另一个页面上使用php函数"包含"。
<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函数中,警报用于测试函数是否被调用,它不是。
有什么想法吗?
答案 0 :(得分:1)
function displayError(var msg) {
document.getElementById('error').innerHTML = msg;
}
将其替换为
function displayError(msg) {
document.getElementById('error').innerHTML = msg;
}
在你的java脚本函数中,你不能像function displayError(var msg)
那样写。
答案 1 :(得分:0)
您的JavaScript中的错误:
function displayError(var msg) {
^^^
document.getElementById('error').innerHTML = msg;
}
您不需要方法参数中的var
关键字。