帮我写一个登录验证,账户密码正确就跳转页面,失败就在当前页面提示账号或密码错误
时间: 2023-05-28 17:01:55 浏览: 82
很高兴为您提供一个简单的示例代码,该代码可以进行基本的登录验证,并根据验证结果在页面上显示相应的消息:
HTML:
```
<form>
<label for="username">用户名:</label>
<input type="text" id="username" name="username" required><br>
<label for="password">密码:</label>
<input type="password" id="password" name="password" required><br>
<button type="submit" id="login-btn">登录</button>
</form>
<div id="message"></div>
```
JavaScript:
```
const form = document.querySelector('form');
const username = form.querySelector('input[type="text"]');
const password = form.querySelector('input[type="password"]');
const message = document.querySelector('#message');
form.addEventListener('submit', function(event) {
event.preventDefault();
const inputUsername = username.value;
const inputPassword = password.value;
if (inputUsername === 'your_username' && inputPassword === 'your_password') {
message.textContent = '';
window.location.href = 'http://example.com/your_page';
} else {
message.textContent = '账号或密码错误';
}
});
```
请注意,这只是一个简单的示例,并且仅限于客户端验证。如果您正在构建一个真正的登录系统,您需要考虑到更多的安全问题,例如使用HTTPS和服务器端验证等。