用户认证系统登录界面

下面是使用HTML和JavaScript实现的一个中文版登录界面,包含登录、注册和修改密码功能。注册成功后会显示提示信息,在登录成功后进入一个大大的欢迎页面。

1.代码展示

html 复制代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户认证系统</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #f4f4f9;
        }
        .container {
            width: 300px;
            padding: 20px;
            background-color: white;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            border-radius: 5px;
            text-align: center;
        }
        h2 {
            margin-bottom: 20px;
        }
        form {
            display: flex;
            flex-direction: column;
        }
        input[type="text"],
        input[type="password"] {
            margin-bottom: 10px;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
            width: 100%;
        }
        button {
            padding: 10px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            width: 100%;
        }
        button:hover {
            background-color: #0056b3;
        }
        .message {
            margin-top: 10px;
        }
        .success {
            color: green;
        }
        .error {
            color: red;
        }
        #welcome-container {
            width: 400px;
            padding: 40px;
            background-color: #d4edda;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            border-radius: 10px;
            text-align: center;
        }
        #welcome-message {
            font-size: 24px;
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
    <div class="container" id="login-container">
        <h2>登录</h2>
        <form id="login-form">
            <input type="text" id="login-username" placeholder="用户名" required>
            <input type="password" id="login-password" placeholder="密码" required>
            <button type="submit">登录</button>
        </form>
        <p class="message error" id="login-error"></p>
        <p class="message"><a href="#" onclick="showRegisterForm(); return false;">注册新账号</a></p>
    </div>

    <div class="container" id="register-container" style="display: none;">
        <h2>注册</h2>
        <form id="register-form">
            <input type="text" id="register-username" placeholder="用户名" required>
            <input type="password" id="register-password" placeholder="密码" required>
            <button type="submit">注册</button>
        </form>
        <p class="message success" id="register-success"></p>
        <p class="message error" id="register-error"></p>
        <p class="message"><a href="#" onclick="showLoginForm(); return false;">返回登录</a></p>
    </div>

    <div class="container" id="change-password-container" style="display: none;">
        <h2>修改密码</h2>
        <form id="change-password-form">
            <input type="password" id="old-password" placeholder="旧密码" required>
            <input type="password" id="new-password" placeholder="新密码" required>
            <button type="submit">修改密码</button>
        </form>
        <p class="message error" id="change-password-error"></p>
        <p class="message"><a href="#" onclick="showWelcomePage(); return false;">返回欢迎页面</a></p>
    </div>

    <div id="welcome-container" style="display: none;">
        <h2 id="welcome-message"></h2>
        <button onclick="showChangePassword()">修改密码</button>
        <button onclick="logout()">注销</button>
    </div>

    <script>
        let users = [];
        let currentUser = null;

        document.getElementById('login-form').addEventListener('submit', function(event) {
            event.preventDefault();
            const username = document.getElementById('login-username').value;
            const password = document.getElementById('login-password').value;
            const user = users.find(user => user.username === username && user.password === password);
            if (user) {
                currentUser = user;
                showWelcomePage();
            } else {
                document.getElementById('login-error').textContent = '无效的用户名或密码';
            }
        });

        document.getElementById('register-form').addEventListener('submit', function(event) {
            event.preventDefault();
            const username = document.getElementById('register-username').value;
            const password = document.getElementById('register-password').value;
            if (!users.find(user => user.username === username)) {
                users.push({ username, password });
                document.getElementById('register-success').textContent = '注册成功!';
                setTimeout(() => {
                    document.getElementById('register-success').textContent = '';
                }, 3000);
                document.getElementById('register-username').value = '';
                document.getElementById('register-password').value = '';
                showLoginForm();
            } else {
                document.getElementById('register-error').textContent = '用户名已存在';
            }
        });

        document.getElementById('change-password-form').addEventListener('submit', function(event) {
            event.preventDefault();
            const oldPassword = document.getElementById('old-password').value;
            const newPassword = document.getElementById('new-password').value;
            if (currentUser && currentUser.password === oldPassword) {
                currentUser.password = newPassword;
                users = users.map(user => 
                    user.username === currentUser.username ? { ...user, password: newPassword } : user
                );
                document.getElementById('change-password-error').textContent = '密码修改成功';
                setTimeout(() => {
                    document.getElementById('change-password-error').textContent = '';
                }, 3000);
                hideChangePassword();
            } else {
                document.getElementById('change-password-error').textContent = '旧密码不正确';
            }
        });

        function showWelcomePage() {
            document.getElementById('login-container').style.display = 'none';
            document.getElementById('register-container').style.display = 'none';
            document.getElementById('change-password-container').style.display = 'none';
            document.getElementById('welcome-container').style.display = 'block';
            document.getElementById('welcome-message').textContent = `欢迎, ${currentUser.username}`;
        }

        function logout() {
            currentUser = null;
            document.getElementById('welcome-container').style.display = 'none';
            document.getElementById('login-container').style.display = 'block';
        }

        function showRegisterForm() {
            document.getElementById('login-container').style.display = 'none';
            document.getElementById('register-container').style.display = 'block';
            document.getElementById('login-error').textContent = ''; // Clear login error message
        }

        function showLoginForm() {
            document.getElementById('register-container').style.display = 'none';
            document.getElementById('login-container').style.display = 'block';
            document.getElementById('register-success').textContent = ''; // Clear registration success message
            document.getElementById('register-error').textContent = ''; // Clear registration error message
        }

        function showChangePassword() {
            document.getElementById('welcome-container').style.display = 'none';
            document.getElementById('change-password-container').style.display = 'block';
        }

        function hideChangePassword() {
            document.getElementById('change-password-container').style.display = 'none';
            document.getElementById('welcome-container').style.display = 'block';
        }
    </script>
</body>
</html>

2.功能说明:

这个应用程序使用纯HTML和JavaScript实现。

  • 包含登录、注册和修改密码功能。
  • 注册成功后会显示一条绿色的成功消息,并在3秒后自动消失。
  • 登录成功后,用户会被带到一个大大的欢迎页面。
  • 用户可以点击"注销"按钮退出登录。
  • 提供了从登录页面跳转到注册页面和从注册页面返回登录页面的链接。
  • 欢迎页面中有一个"修改密码"的按钮,可以跳转到修改密码页面。

3.效果图

相关推荐
崔庆才丨静觅7 小时前
hCaptcha 验证码图像识别 API 对接教程
前端
passerby60617 小时前
完成前端时间处理的另一块版图
前端·github·web components
掘了8 小时前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
崔庆才丨静觅8 小时前
实用免费的 Short URL 短链接 API 对接说明
前端
崔庆才丨静觅8 小时前
5分钟快速搭建 AI 平台并用它赚钱!
前端
崔庆才丨静觅8 小时前
比官方便宜一半以上!Midjourney API 申请及使用
前端
Moment8 小时前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端
崔庆才丨静觅9 小时前
刷屏全网的“nano-banana”API接入指南!0.1元/张量产高清创意图,开发者必藏
前端
剪刀石头布啊9 小时前
jwt介绍
前端
爱敲代码的小鱼9 小时前
AJAX(异步交互的技术来实现从服务端中获取数据):
前端·javascript·ajax