验证码输入弹窗

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .captcha-pop {
      width: 100%;
      height: 100vh;
      position: fixed;
      top: 0;
      left: 0;
      background: rgba(0, 0, 0, 0.70);
    }

    .captcha-wrapper {
      position: fixed;
      bottom: 0;
      left: 0;
      width: 100%;
      min-height: 330px;
      background: #ffffff;
      border-radius: 10px 10px 0 0;
      padding: 16px;
      box-sizing: border-box;
    }

    .captcha-header {
      width: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 50px;
      position: relative;
      font-size: 18px;
      font-family: PingFang SC, PingFang SC-500;
      font-weight: 500;
      color: #333333;
    }

    .captcha-header-close {
      position: absolute;
      top: 0;
      right: 0;
      width: 23px;
      height: 23px;
    }

    .captcha-img {
      width: 100%;
      height: 100%;
    }

    .captcha-pic {
      width: 100%;
      height: 48px;
      display: flex;
      justify-content: center;
      align-items: center;
      margin: 8px;
    }


    .captcha-pic-img {
      width: 90px;
      height: 40px;
    }

    .captcha-pic-reload {
      height: 20px;
      font-size: 14px;
      font-family: PingFang SC, PingFang SC-400;
      font-weight: 400;
      text-align: right;
      color: #18A4EA;
      margin-left: 10px;
    }

    .captcha-content {
      display: flex;
      justify-content: space-evenly;
      align-items: center;
      gap: 5px;
      padding: 10px 20px;
      box-sizing: border-box;
      /* 你可以根据需要调整格子之间的间距 */
    }

    .captcha-digit {
      width: 40px;
      /* 你可以根据需要调整输入框的宽度 */
      height: 40px;
      padding: 5px;
      text-align: center;
      background: #edf2f5;
      border-radius: 8px;
      border: 1px #edf2f5 solid;
      font-size: 16px;
      font-family: PingFang SC, PingFang SC-400;
    }

    .captcha-tip {
      width: 100%;
      height: 20px;
      display: flex;
      justify-content: center;
      align-items: center;
      padding: 0 25px 0 10px;
      box-sizing: border-box;
      font-size: 14px;
      font-family: PingFang SC, PingFang SC-400;
      font-weight: 400;
      color: #f0342f;
    }

    .captcha-tip-warning {
      display: none;
    }

    .captcha-tip-reload {
      flex: 1;
      height: 20px;
      font-size: 14px;
      font-family: PingFang SC, PingFang SC-400;
      font-weight: 400;
      text-align: right;
      color: #18A4EA;
    }

    .captcha-btn {
      width: 300px;
      height: 44px;
      background: #abc6d4;
      border-radius: 4px;
      margin: 0 auto;
      font-size: 16px;
      font-family: PingFang SC, PingFang SC-500;
      font-weight: 500;
      color: #ffffff;
      display: flex;
      justify-content: center;
      align-items: center;
      margin-top: 20px;
    }
  </style>
</head>

<body>
  <div class="captcha-pop" id="captcha-pop">
    <div class="captcha-wrapper">
      <div class="captcha-header">
        <div class="captcha-header-title">
          请输入下方图像验证码
        </div>
        <div class="captcha-header-close" id="captcha-close">
          <img src="https://app.fmcc.com.cn/group1/M00/04/48/CizIPWY-2eWAEvOPAAACIOdETlg984.png" alt="" srcset=""
            class="captcha-img">
        </div>
      </div>
      <div class="captcha-pic">
        <div class="captcha-pic-img">
          <img src="https://app.fmcc.com.cn/group1/M00/04/48/CizIPWY_FZKALQxYAAAlS22O-L4757.png" alt="" srcset=""
            class="captcha-img">
        </div>
        <div class="captcha-pic-reload">
          看不清?
        </div>
      </div>
      <div class="captcha-content" id="captcha-content">
        <input type="text" class="captcha-digit" maxlength="1" />
        <input type="text" class="captcha-digit" maxlength="1" />
        <input type="text" class="captcha-digit" maxlength="1" />
        <input type="text" class="captcha-digit" maxlength="1" />
      </div>
      <div class="captcha-tip">
        <div class="captcha-tip-warning" id="captcha-warning">
          *请输入正确的验证码
        </div>
      </div>
      <div class="captcha-btn" id="captcha-btn">
        确定
      </div>
    </div>
  </div>
</body>

<script>
  document.getElementById('captcha-content').addEventListener('click', function () {
    if (document.querySelectorAll('.captcha-digit')[0].value == '') {
      document.querySelectorAll('.captcha-digit')[0].focus();
    } else if (document.querySelectorAll('.captcha-digit')[1].value == '') {
      document.querySelectorAll('.captcha-digit')[1].focus();
    } else if (document.querySelectorAll('.captcha-digit')[2].value == '') {
      document.querySelectorAll('.captcha-digit')[2].focus();
    } else if (document.querySelectorAll('.captcha-digit')[3].value == '') {
      document.querySelectorAll('.captcha-digit')[3].focus();
    } else {
      document.querySelectorAll('.captcha-digit')[3].focus();
    }
  })
  document.querySelectorAll('.captcha-digit').forEach((input, index) => {
    input.addEventListener('input', function () {
      if (index == 3 && document.querySelectorAll('.captcha-digit')[3].value != '') {
        document.getElementById('captcha-btn').style.background = '#16a3ea';
      } else {
        document.getElementById('captcha-btn').style.background = '#abc6d4';
        document.getElementById('captcha-warning').style.display = 'none';
      }
      if (document.querySelectorAll('.captcha-digit')[0].value == '' & index > 0) {
        document.querySelectorAll('.captcha-digit')[0].focus();
        document.querySelectorAll('.captcha-digit')[0].value = this.value;
        document.querySelectorAll('.captcha-digit')[1].value = '';
        document.querySelectorAll('.captcha-digit')[2].value = '';
        document.querySelectorAll('.captcha-digit')[3].value = '';
        document.querySelectorAll('.captcha-digit')[1].focus();
        return;
      }
      if (document.querySelectorAll('.captcha-digit')[1].value == '' & index > 1) {
        document.querySelectorAll('.captcha-digit')[1].focus();
        document.querySelectorAll('.captcha-digit')[1].value = this.value;
        document.querySelectorAll('.captcha-digit')[2].value = '';
        document.querySelectorAll('.captcha-digit')[3].value = '';
        document.querySelectorAll('.captcha-digit')[2].focus();
        return;
      }
      if (document.querySelectorAll('.captcha-digit')[2].value == '' & index > 2) {
        document.querySelectorAll('.captcha-digit')[2].focus();
        document.querySelectorAll('.captcha-digit')[2].value = this.value;
        document.querySelectorAll('.captcha-digit')[3].value = '';
        document.querySelectorAll('.captcha-digit')[1].focus();
        return;
      }
      if (this.value.length === 1 && index < 3) {
        // 如果当前输入框已满且不是最后一个,则跳到下一个输入框  
        document.querySelectorAll('.captcha-digit')[index + 1].focus();
      }

    });

  });
  document.querySelectorAll('.captcha-digit').forEach((input, index) => {
    input.addEventListener('keydown', function (event) {
      // 检查是否按下了删除键(Backspace 或 Delete)  
      if (event.key === 'Backspace' || event.key === 'Delete') {
        // 按下了删除键,执行你的逻辑  
        if (document.querySelectorAll('.captcha-digit')[0].value == '') {
          document.querySelectorAll('.captcha-digit')[0].focus();
        } else if (document.querySelectorAll('.captcha-digit')[0].value != '' && document.querySelectorAll('.captcha-digit')[1].value == '') {
          document.querySelectorAll('.captcha-digit')[0].value = '';
          document.querySelectorAll('.captcha-digit')[0].focus();
        } else if (document.querySelectorAll('.captcha-digit')[1].value != '' && document.querySelectorAll('.captcha-digit')[2].value == '') {
          document.querySelectorAll('.captcha-digit')[1].value = '';
          document.querySelectorAll('.captcha-digit')[1].focus();
          return
        } else if (document.querySelectorAll('.captcha-digit')[2].value != '' && document.querySelectorAll('.captcha-digit')[3].value == '') {
          document.querySelectorAll('.captcha-digit')[2].value = '';
          document.querySelectorAll('.captcha-digit')[2].focus();
          return
        }
      }
    });

    // 你可以在这里添加更多的验证逻辑  
  });

  document.getElementById('captcha-btn').addEventListener('click', function () {
    let inputValue = '';
    document.querySelectorAll('.captcha-digit').forEach(input => {
      inputValue = inputValue + input.value;
    })
    if (inputValue.length < 4) {
      return;
    }
    console.log(inputValue, inputValue.toLocaleLowerCase(), '====123')
    if (inputValue.toLocaleLowerCase() != '5ru6') {
      document.getElementById('captcha-warning').style.display = 'block';
      return;
    }
    if (inputValue.toLocaleLowerCase() == '5ru6') {
     console.log('验证码校验成功!进入下一步。');
    }
  })

  document.getElementById('captcha-close').addEventListener('click', function () {
    document.getElementById('captcha-pop').style.display = 'none';
  })
</script>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
这个问题涉及到 Android Studio 的开发,需要使用 Java 语言来实现。首先,在 XML 布局文件中,我们需要创建一个编辑框和一个按钮,用于输入手机号码和触发获取验证码的操作。代码如下: ```xml <EditText android:id="@+id/phone_edittext" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="phone" android:hint="请输入手机号"/> <Button android:id="@+id/code_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="获取验证码"/> ``` 接下来,在 Java 代码中,我们需要给按钮设置一个点击事件,当用户点击按钮时,生成一个随机的六位数验证码,并将这个验证码弹窗的形式展示给用户。同时,弹窗中需要显示用户输入的手机号码。代码如下: ```java Button codeButton = findViewById(R.id.code_button); codeButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 生成随机六位数验证码 int code = (int) ((Math.random() * 9 + 1) * 100000); // 获取用户输入的手机号码 EditText phoneEditText = findViewById(R.id.phone_edittext); String phone = phoneEditText.getText().toString(); // 在弹窗中展示手机号码和验证码 AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this); alertDialogBuilder.setMessage("请记住验证码,您的手机号是 " + phone + ",本次登录的验证码为 " + code + ",请输入验证码"); alertDialogBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { Toast.makeText(MainActivity.this,"验证码已发送",Toast.LENGTH_LONG).show(); } }); AlertDialog alertDialog = alertDialogBuilder.create(); alertDialog.show(); } }); ``` 这样,当用户点击按钮时,就会生成一个随机的六位数验证码,并在弹窗中展示给用户,同时将用户输入的手机号码也展示在弹窗中。为了保证安全性,生成的验证码可以通过短信发送给用户,用户在输入验证码后才能成功登录。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Root1216

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

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

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

打赏作者

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

抵扣说明:

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

余额充值