news 2026/6/9 12:39:03

Vue3登录注册验证码实战

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Vue3登录注册验证码实战

以下是使用 Vue.js 实现登录、注册和验证码功能的完整代码示例。我将逐步解释关键部分,并提供可运行的 HTML 文件代码。代码使用了 Vue 3 的 Composition API,并模拟了验证码生成(真实应用中应通过后端 API 获取验证码)。

思路说明

  1. 功能需求

    • 登录:用户输入用户名、密码和验证码。
    • 注册:切换到注册表单,输入用户名、密码和验证码。
    • 验证码:前端生成随机验证码(简单模拟),用户需正确输入。
    • 表单切换:提供按钮在登录和注册模式间切换。
  2. 安全注意

    • 真实应用中,验证码应由后端生成(避免前端伪造),登录/注册请求需发送到后端 API。
    • 本示例为简化,在前端模拟验证码和成功响应。

完整代码示例

将以下代码保存为 HTML 文件(例如index.html),在浏览器中打开即可运行。

<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Vue登录注册与验证码</title> <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script> <style> body { font-family: Arial, sans-serif; padding: 20px; } form { max-width: 400px; margin: 0 auto; } div { margin-bottom: 15px; } label { display: block; margin-bottom: 5px; } input { width: 100%; padding: 8px; } button { padding: 8px 15px; margin-right: 10px; } .captcha-container { display: flex; align-items: center; } .captcha-value { font-size: 20px; margin: 0 10px; padding: 5px; background: #eee; } .message { color: red; margin-top: 10px; } </style> </head> <body> <div id="app"> <h1>{{ isRegistering ? '用户注册' : '用户登录' }}</h1> <form @submit.prevent="handleSubmit"> <div> <label for="username">用户名:</label> <input type="text" id="username" v-model="username" required> </div> <div> <label for="password">密码:</label> <input type="password" id="password" v-model="password" required> </div> <div class="captcha-container"> <label for="captcha">验证码:</label> <input type="text" id="captcha" v-model="captchaInput" required> <span class="captcha-value">{{ captchaValue }}</span> <button type="button" @click="generateCaptcha">刷新</button> </div> <div> <button type="button" @click="toggleForm">{{ isRegistering ? '切换到登录' : '切换到注册' }}</button> <button type="submit">{{ isRegistering ? '注册' : '登录' }}</button> </div> <p v-if="message" class="message">{{ message }}</p> </form> </div> <script> const { createApp, ref } = Vue; const app = createApp({ setup() { // 响应式数据 const username = ref(''); const password = ref(''); const captchaInput = ref(''); const captchaValue = ref(''); const isRegistering = ref(false); const message = ref(''); // 生成随机验证码(4位数字) const generateCaptcha = () => { const chars = '0123456789'; let result = ''; for (let i = 0; i < 4; i++) { result += chars.charAt(Math.floor(Math.random() * chars.length)); } captchaValue.value = result; }; // 初始化生成验证码 generateCaptcha(); // 切换登录/注册表单 const toggleForm = () => { isRegistering.value = !isRegistering.value; generateCaptcha(); // 刷新验证码 message.value = ''; // 清空消息 }; // 处理表单提交 const handleSubmit = () => { // 验证码检查 if (captchaInput.value !== captchaValue.value) { message.value = '验证码错误!'; generateCaptcha(); // 刷新验证码 return; } // 模拟登录/注册成功 if (isRegistering.value) { message.value = `注册成功,用户名: ${username.value}`; } else { message.value = `登录成功,用户名: ${username.value}`; } // 清空表单(可选) setTimeout(() => { username.value = ''; password.value = ''; captchaInput.value = ''; generateCaptcha(); }, 2000); }; return { username, password, captchaInput, captchaValue, isRegistering, message, generateCaptcha, toggleForm, handleSubmit }; } }); app.mount('#app'); </script> </body> </html>

代码解释

  1. Vue 初始化

    • 使用 Vue 3 的createAppref创建响应式应用。
    • 数据包括username,password,captchaInput,captchaValue,isRegistering(控制表单模式),message(反馈信息)。
  2. 验证码功能

    • generateCaptcha方法:生成 4 位随机数字验证码,存储在captchaValue
    • 页面加载时自动生成验证码,用户可点击“刷新”按钮重新生成。
    • 提交表单时,检查用户输入 (captchaInput) 是否与captchaValue匹配。
  3. 表单切换

    • toggleForm方法:切换isRegistering状态,改变表单标题和按钮文本。
    • 使用 Vue 的v-if和三元表达式动态显示内容。
  4. 表单提交

    • handleSubmit方法:使用@submit.prevent阻止默认提交行为。
    • 先验证验证码,错误时显示消息并刷新验证码。
    • 验证成功后,模拟登录或注册成功消息,并清空表单(可选)。

运行说明

  1. 复制上述代码到index.html文件。
  2. 在浏览器中打开文件。
  3. 测试:
    • 默认是登录表单,输入用户名、密码和验证码。
    • 点击“切换到注册”进入注册模式。
    • 验证码错误时会提示,正确时显示成功消息。

扩展建议

  • 后端集成:在真实项目中,使用 Axios 发送请求到后端 API,例如:
    // 伪代码示例 import axios from 'axios'; const login = async () => { try { const response = await axios.post('/api/login', { username, password }); console.log(response.data); } catch (error) { console.error('登录失败', error); } };

    编程语言C++wap.chichenghj.com++c语言的魅力
    编程语言C++wap.xytdyf.com++c语言的魅力
    编程语言C++wap.huishangda.cn++c语言的魅力
    编程语言C++wap.jnlangrui.com++c语言的魅力
    编程语言C++wap.hisee.top++c语言的魅力
    编程语言C++m.chichenghj.com++c语言的魅力
    编程语言C++www.chichenghj.com++c语言的魅力
    编程语言C++www.blog.chichenghj.com++c语言的魅力
    编程语言C++www.share.chichenghj.com++c语言的魅力
    编程语言C++read.share.chichenghj.com++c语言的魅力
    编程语言C++www.xytdyf.com++c语言的魅力
    编程语言C++www.blog.xytdyf.com++c语言的魅力
    编程语言C++www.share.xytdyf.com++c语言的魅力
    编程语言C++read.share.xytdyf.com++c语言的魅力
    编程语言C++m.xytdyf.com++c语言的魅力
    编程语言C++www.huishangda.cn++c语言的魅力
    编程语言C++www.blog.huishangda.cn++c语言的魅力
    编程语言C++www.share.huishangda.cn++c语言的魅力
    编程语言C++read.share.huishangda.cn++c语言的魅力
    编程语言C++m.huishangda.cn++c语言的魅力
    编程语言C++www.jnlangrui.com++c语言的魅力
    编程语言C++www.blog.jnlangrui.com++c语言的魅力
    编程语言C++www.share.jnlangrui.com++c语言的魅力
    编程语言C++read.share.jnlangrui.com++c语言的魅力
    编程语言C++m.jnlangrui.com++c语言的魅力

  • 验证码加强:后端应生成图形或复杂验证码,并验证用户输入。
  • 输入验证:添加更多验证规则(如密码强度)。

此代码提供了一个基础框架,您可以根据需求扩展功能!

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/7 10:53:20

结构化数据的炼金术:信息抽取 (Information Extraction) 深度研究报告

1. 引言&#xff1a;从非结构化混沌到结构化秩序 在当今的数字化时代&#xff0c;人类社会正以前所未有的速度通过各种渠道生成数据。从社交媒体的碎片化表达&#xff0c;到企业内部的财务报表&#xff0c;再到医疗系统的临床记录&#xff0c;数据的洪流无处不在。然而&#x…

作者头像 李华
网站建设 2026/6/7 11:10:45

学术降重必备:AI生成论文工具精选

工具名称 核心优势 适用场景 aicheck 快速降AIGC率至个位数 AIGC优化、重复率降低 aibiye 智能生成论文大纲 论文结构与内容生成 askpaper 文献高效整合 开题报告与文献综述 秒篇 降重效果显著 重复率大幅降低 一站式论文查重降重 查重改写一站式 完整论文优化…

作者头像 李华
网站建设 2026/6/7 11:29:46

安装docker desktop 后出现WSL版本低需要更新问题

记录下 安装使用过程中遇到的问题 还未解决 在网上搜索 需要以管理员身份运行 cmd 黑窗口 执行 wsl --update命令 更新WSL 等晚上下班回家试一下 各位大神们你们也遇到类似的问题了吗&#xff1f;是不是这么简单就解决了&#xff1f;可行&#xff1f; 等我下班回家亲自验证下 看…

作者头像 李华
网站建设 2026/6/7 11:52:12

软考-系统架构师-信息安全技术基础知识(三)

八、认证服务 8.1、PKI/CA 公钥基础设施 8.1.1、定义 PKI/CA (Public Key Infrastructure / Certificate Authority)&#xff0c;即公钥基础设施/认证中心&#xff08;证书颁发机构&#xff09;。 8.1.2、核心组件 CA 中心&#xff1a;负责颁发数字证书的权威机构。 服务…

作者头像 李华
网站建设 2026/6/8 15:51:40

基建收缩下的生存法则:哪些战略机构能帮建工企业破解“百亿枷锁”?

在当前基建收缩的背景下&#xff0c;建工企业不仅面临着市场需求萎缩的压力&#xff0c;还需要寻找有效的转型路径。战略咨询机构正是这些企业破解“百亿枷锁”的重要合作伙伴。通过专业的市场分析和定制化解决方案&#xff0c;咨询机构能够帮助建工企业重新审视自身业务&#…

作者头像 李华
网站建设 2026/6/8 14:36:18

学长亲荐2026继续教育AI论文平台TOP9:开题文献综述全攻略

学长亲荐2026继续教育AI论文平台TOP9&#xff1a;开题文献综述全攻略 2026年继续教育AI论文平台测评&#xff1a;为何需要这份权威榜单&#xff1f; 在当前学术研究日益数字化的背景下&#xff0c;继续教育领域的学员和研究者面临着前所未有的挑战。从选题构思到文献综述&#…

作者头像 李华