前端验证码示例

<!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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Tin9898

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

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

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

打赏作者

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

抵扣说明:

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

余额充值