前端验证码示例

<!DOCTYPE html>
<html>
<head>
  <title>验证码示例</title>
  <style>
    #captcha {
      border: 1px solid #000;
      width: 50px;
      display: inline-block;
      user-select: none; /* 禁止选择文本 */
    }
  </style>
</head>
<body>
  <h2>请输入账号密码和验证码</h2>
  <form id="loginForm">
    <div>
      <label for="username">账号:</label>
      <input type="text" id="username" onblur="validateUsername()">
      <span id="usernameError" style="color: red"></span>
    </div>
    <div>
      <label for="password">密码:</label>
      <input type="password" id="password" onblur="validatePassword()">
      <input type="checkbox" id="showPassword" onchange="togglePasswordVisibility()">
      <label for="showPassword">显示密码</label>
      <span id="passwordError" style="color: red"></span>
    </div>
    <div>
      <label for="captcha">验证码:</label>
      <input type="text" id="captchaInput" oncopy="return false" onpaste="return false">
      <span id="captcha" style="border: 1px solid #000; padding: 2px 5px;"></span>
      <button type="button" onclick="refreshCaptcha()">刷新</button>
    </div>
    <button type="button" onclick="login()">登录</button>
  </form>

  <script>
    // 生成4位随机验证码
    function generateCaptcha() {
      let captcha = '';
      const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
      for (let i = 0; i < 4; i++) {
        captcha += characters.charAt(Math.floor(Math.random() * characters.length));
      }
      return captcha;
    }

    // 刷新验证码
    function refreshCaptcha() {
      const captcha = generateCaptcha();
      document.getElementById('captcha').innerText = captcha;
    }

    // 验证账号
    function validateUsername() {
      const username = document.getElementById('username').value;
      const usernameError = document.getElementById('usernameError');
      if (!/^\d{6}$/.test(username)) {
        usernameError.innerText = '账号必须为6位数字';
      } else {
        usernameError.innerText = '';
      }
    }

    // 验证密码
    function validatePassword() {
      const password = document.getElementById('password').value;
      const passwordError = document.getElementById('passwordError');
      if (password.length < 8 || password.length > 12) {
        passwordError.innerText = '密码长度必须在8到12位之间';
      } else {
        passwordError.innerText = '';
      }
    }

    // 切换密码可见性
    function togglePasswordVisibility() {
      const passwordInput = document.getElementById('password');
      passwordInput.type = document.getElementById('showPassword').checked ? 'text' : 'password';
    }

    // 登录验证
    function login() {
      const username = document.getElementById('username').value;
      const password = document.getElementById('password').value;
      const inputCaptcha = document.getElementById('captchaInput').value.toLowerCase();
      const generatedCaptcha = document.getElementById('captcha').innerText.toLowerCase();

      // 验证账号和密码
      if (!/^\d{6}$/.test(username)) {
        document.getElementById('usernameError').innerText = '账号必须为6位数字';
        return;
      } else {
        document.getElementById('usernameError').innerText = '';
      }
      if (password.length < 8 || password.length > 12) {
        document.getElementById('passwordError').innerText = '密码长度必须在8到12位之间';
        return;
      } else {
        document.getElementById('passwordError').innerText = '';
      }

      // 验证验证码
      if (inputCaptcha === generatedCaptcha) {
        alert('验证码正确,跳转至百度页面');
        window.location.href = 'https://www.baidu.com'; // 跳转至百度页面
      } else {
        alert('验证码错误');
      }
    }

    // 初始化时生成验证码
    document.getElementById('captcha').innerText = generateCaptcha();
  </script>
</body>
</html>

前端小程序中发送手机号验证码通常涉及到异步请求和处理用户输入验证的过程。以下是一个基本的微信小程序获取并显示验证码示例代码: ```html <view class="code-input"> <input type="tel" placeholder="请输入手机号" bindInput="handlePhoneInput" /> <button open-type="getPhoneNumber" bindgetphonenumber="sendCode">获取验证码</button> <text>{{showCode}}</text> <!-- 显示验证码 --> </view> <script> Page({ data: { phone: '', code: '', showCode: '点击获取', // 其他配置如请求接口等... }, handlePhoneInput(e) { this.setData({ phone: e.detail.value }); }, sendCode(event) { if (!this.data.phone) { wx.showToast({ title: '请输入手机号', icon: 'none' }); return; } // 模拟发送请求 const phoneNumber = this.data.phone; setTimeout(() => { const randomCode = Math.floor(100000 + Math.random() * 900000); this.setData({ code: randomCode, showCode: `验证码已发送,为${randomCode}` }); // 这里需要替换为实际的发送验证码API // wx.request({ // url: 'your-server-url/send-code', // 替换为你的服务器地址 // data: { mobile: phoneNumber, code: randomCode }, // method: 'POST', // success: () => { // console.log('验证码发送成功'); // }, // fail: () => { // console.error('验证码发送失败'); // } // }); }, 2000); // 模拟延迟2秒响应 }, verifyCode(e) { // 用户输入验证码后可以添加此事件处理函数进行验证 // 如果验证通过,可以继续后续流程 } }) </script> ``` 这个例子中,当用户点击“获取验证码”按钮时,会模拟一个延迟2秒的请求发送到服务器,并更新显示的文字。实际应用中,你需要将`wx.request`替换为你的后端提供的发送验证码接口,并在`verifyCode`函数中添加用户输入验证码后的验证过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tin9898

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值