用户认证系统登录界面

下面是使用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.效果图

相关推荐
Revolution6134 分钟前
React 组件重新渲染时,到底重新执行了什么
前端·react.js·面试
林焱_RPAAI38 分钟前
影刀RPA操作数据库进阶:事务处理与批量写入优化
css
用户2181697049302 小时前
Flutter(四)Dart语法 空安全 运算符 流程控制
前端
许彰午2 小时前
政务督办的分合模式:主办协办的并发审批
前端·javascript·政务
用户69371750013842 小时前
AI时代,程序员该往哪走?
前端·后端
ttwuai2 小时前
GoFrame 后台日志清空失败:无 WHERE 删除为什么被拦住
前端·golang
易筋紫容2 小时前
创建型模式:对象的诞生艺术
开发语言·前端·javascript
半句唐诗3 小时前
我是如何通过 Access Token 成功发布第一个 npm 包的
前端·npm·node.js
程序员黑豆3 小时前
鸿蒙应用开发:@Provider 与 @Consumer 跨组件双向同步详解
前端·harmonyos
paopaokaka_luck3 小时前
基于springboot3+vue3的智能文库平台(AI智能搜索、AI智能汇总、实时在线状态展示、多格式文档预览与富文本编辑、Echarts图形化分析)
前端·网络·spring boot·网络协议·echarts