使用html和css网页设置完成基于localstorage的注册和登录
时间: 2023-08-09 19:08:49 浏览: 72
好的,以下是一个基于Local Storage的注册和登录页面的HTML和CSS代码:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Registration and Login Page</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
}
.container {
max-width: 500px;
margin: auto;
padding: 20px;
background-color: #fff;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
}
h1 {
text-align: center;
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 8px;
}
input[type="text"],
input[type="password"] {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
box-sizing: border-box;
border: none;
border-radius: 4px;
background-color: #f8f8f8;
}
button[type="submit"] {
background-color: #4CAF50;
color: #fff;
padding: 12px 20px;
margin-top: 20px;
border: none;
border-radius: 4px;
cursor: pointer;
}
button[type="submit"]:hover {
background-color: #3e8e41;
}
#error {
color: red;
margin-top: 10px;
}
#success {
color: green;
margin-top: 10px;
}
#login-form {
display: none;
}
#login-link {
text-align: center;
margin-top: 20px;
}
#login-link a {
color: blue;
text-decoration: underline;
cursor: pointer;
}
</style>
</head>
<body>
<div class="container">
<h1>Registration and Login Page</h1>
<form id="register-form">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
<button type="submit">Register</button>
<div id="error"></div>
<div id="success"></div>
</form>
<form id="login-form">
<label for="login-username">Username:</label>
<input type="text" id="login-username" name="login-username" required>
<label for="login-password">Password:</label>
<input type="password" id="login-password" name="login-password" required>
<button type="submit">Login</button>
<div id="error"></div>
<div id="success"></div>
</form>
<div id="login-link">
<a id="toggle-login" onclick="toggleLogin()">Already have an account? Login here.</a>
</div>
</div>
<script>
const registerForm = document.getElementById("register-form");
const loginForm = document.getElementById("login-form");
const toggleLoginLink = document.getElementById("toggle-login");
const errorDivs = document.querySelectorAll("#error");
const successDivs = document.querySelectorAll("#success");
// Toggle between the registration and login forms
function toggleLogin() {
registerForm.style.display = "none";
loginForm.style.display = "block";
toggleLoginLink.innerHTML = "Don't have an account? Register here.";
}
function toggleRegister() {
loginForm.style.display = "none";
registerForm.style.display = "block";
toggleLoginLink.innerHTML = "Already have an account? Login here.";
}
// Register a new user
function registerUser(event) {
event.preventDefault();
const username = document.getElementById("username").value;
const password = document.getElementById("password").value;
const users = JSON.parse(localStorage.getItem("users")) || [];
// Check if username is already taken
for (let i = 0; i < users.length; i++) {
if (users[i].username === username) {
showError("Username is already taken.");
return;
}
}
// Add new user to localStorage
users.push({ username, password });
localStorage.setItem("users", JSON.stringify(users));
showSuccess("Registration successful. You may now login.");
toggleLogin();
}
// Authenticate user and login
function loginUser(event) {
event.preventDefault();
const username = document.getElementById("login-username").value;
const password = document.getElementById("login-password").value;
const users = JSON.parse(localStorage.getItem("users")) || [];
// Check if username and password match
for (let i = 0; i < users.length; i++) {
if (users[i].username === username && users[i].password === password) {
showSuccess("Login successful.");
return;
}
}
showError("Invalid username or password. Please try again.");
}
function showError(errorMessage) {
for (let i = 0; i < errorDivs.length; i++) {
errorDivs[i].innerHTML = errorMessage;
errorDivs[i].style.display = "block";
}
for (let i = 0; i < successDivs.length; i++) {
successDivs[i].style.display = "none";
}
}
function showSuccess(successMessage) {
for (let i = 0; i < successDivs.length; i++) {
successDivs[i].innerHTML = successMessage;
successDivs[i].style.display = "block";
}
for (let i = 0; i < errorDivs.length; i++) {
errorDivs[i].style.display = "none";
}
}
registerForm.addEventListener("submit", registerUser);
loginForm.addEventListener("submit", loginUser);
</script>
</body>
</html>
```
这个页面包含两个表单,一个用于注册,另一个用于登录。它还包含一个链接,以便在这两个表单之间切换。它使用JavaScript来验证用户并将其存储在本地存储中。请注意,此代码仅用于演示目的,不应用于生产环境。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)