我需要有一个验证登录表单,登录后页面需要重定向到 html 页面。我还需要硬编码用户名和密码,因此只有使用正确的用户名/密码,页面才会重定向。如果它没有正确的用户/通行证,那么我需要一个警报。
我尝试了很多变体,但我知道我遗漏了一些东西或使用了错误的代码。请帮忙!:) 谢谢!到目前为止,这是我的代码。
JS:
function validateForm() {
'use strict';
// Get references to the form elements:
var email = document.getElementById('email');
var password = document.getElementById('password');
// Validate the login
if ((email.value.length > 0) && (password.value.length > 0)) {
return true;
} else {
alert('Please complete the form!');
return false;
}
}
function check(form) {
var emailArray = ("myemail@live.com", "");
var passwordArray = ("MyLogIn", "");
if (email.value == "email" && password.value == "password") {
window.open('myaccount.html');
} else {
alert('Please enter correct username or password!');
return false;
}
}
function init() {
'use strict';
// Confirm that document.getElementById() can be used:
if (document && document.getElementById) {
var loginForm = document.getElementById('lgform');
loginForm.onsubmit = validateForm;
}
}
window.onload = init;
HTML:
<form action="" method="post" id="lgform">
<fieldset>
<legend>Login</legend>
<div>
<label for="email">Email Address</label>
<input type="email" name="email" id="email" required>
</div>
<div>
<label for="password">Password</label>
<input type="password" name="password" id="password" required>
</div>
<div>
<label for="submit"></label>
<input type="submit" value="Login" id="submit">
</div>
</fieldset>
</form>