手机号验证码重新发送

前文叙述

很久以前做的一个 demo ,纯 HTML 、CSS、js 制作,一定时间段之后才可以重新发送验证码,如 60s

后再次发送验证码,在该时间段内发送验证码按钮为禁用状态,实战开发过程也亦是同理,因此记录一手。

一、效果图:

二、实现代码:

bash 复制代码
<!DOCTYPE html>
<html>
<head>
  <title>登录表单</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      background-color: #f2f2f2;
      min-height: 100vh;
      display: flex;
	  justify-content: center;
	  align-items: center;
	  border-
    }

    .container {
      max-width: 400px;
      min-width: 400px;
      margin: auto;
      padding: 20px;
      background-color: #fff;
      border-radius: 5px;
      box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
    }

    h2 {
      text-align: center;
      margin-bottom: 30px;
    }

    .form-group {
      margin-bottom: 20px;
    }

    .form-group-inline {
      display: flex;
      align-items: center;
    }

    label {
      display: block;
      font-weight: bold;
      margin-bottom: 5px;
      flex: 0 0 100px;
    }

    input[type="text"],
    input[type="password"] {
      flex: 1;
      padding: 10px;
      border: 1px solid #ddd;
      border-radius: 4px;
      box-sizing: border-box;
    }
    input[type="text"]:fous{
      border-color: #ebecff;
    }

    .btn {
      display: inline-block;
      padding: 10px 20px;
      background-color: #4caf50;
      color: #fff;
      text-align: center;
      text-decoration: none;
      border-radius: 4px;
      cursor: pointer;
      transition: background-color 0.3s;
    }
    #code {
	  width: 100px;
	  margin-right: 30px;
    }

    .btn:hover {
      background-color: #45a049;
    }

    .btn-disabled {
      background-color: #ccc;
      cursor: not-allowed;
    }
  </style>
</head>
<body>
  <div class="container">
    <h2>登录</h2>
    <div class="form-group form-group-inline">
      <label for="phone">手机号码:</label>
      <input type="text" id="phone" placeholder="请输入手机号码">
    </div>
    <div class="form-group form-group-inline">
      <label for="code">验证码:</label>
      <input type="text" id="code" placeholder="请输入验证码">
	  <button id="sendCode" class="btn">发送验证码</button>
      
     
    </div>
    <button id="loginBtn" class="btn">登录</button>
  </div>

  <script>
    // 获取DOM元素
    const phoneInput = document.getElementById('phone');
    const codeInput = document.getElementById('code');
    const sendCodeBtn = document.getElementById('sendCode');
    const loginBtn = document.getElementById('loginBtn');

    let timer;
    let countdown = 60;
    
    function validatePhoneNumber(phoneNumber) {
	  // 使用正则表达式进行手机号码校验
	  let regex = /^1[0-9]{10}$/;
	  return regex.test(phoneNumber);
	}
	
	function validateVerificationCode(code) {
	  // 使用正则表达式进行六位数验证码校验
	  let regex = /^\d{6}$/;
	  return regex.test(code);
}

    // 发送验证码按钮点击事件
    sendCodeBtn.addEventListener('click', () => {
      if (!phoneInput.value) {
        alert('请输入手机号码');
        return;
      }
      if (!this.validatePhoneNumber(phoneInput.value)) {
        alert("请输入合法手机号");
		return;
      }

      // 禁用发送验证码按钮
      sendCodeBtn.disabled = true;
      sendCodeBtn.classList.add('btn-disabled');

      // 开始倒计时
      countdown = 10;
      sendCodeBtn.innerText = `${countdown}s`;
      sendCodeBtn.style.display = 'block';
      timer = setInterval(() => {
        countdown--;
        if (countdown === 0) {
          // 倒计时结束,恢复发送验证码按钮状态
          clearInterval(timer);
       
          sendCodeBtn.disabled = false;
          sendCodeBtn.classList.remove('btn-disabled');
          sendCodeBtn.innerText = "发送验证码";
          
        } else {
          sendCodeBtn.innerText = `${countdown}s`;
        }
      }, 1000);

      // 模拟发送验证码的逻辑,这里使用了setTimeout
      setTimeout(() => {
        alert('验证码已发送,请注意查收');
      }, 2000);
    });

    // 登录按钮点击事件
    loginBtn.addEventListener('click', () => {
      // 检查手机号和验证码是否为空
      if (!phoneInput.value || !this.validatePhoneNumber(phoneInput.value)) {
        alert('请输入合法手机号');
        return;
      }
	  if (!this.validateVerificationCode(codeInput.value)) {
        alert('请输入合法验证码');
        return;
      }

    });

  </script>
</body>
</html>
相关推荐
Running_slave11 分钟前
你应该了解的TCP滑窗
前端·网络协议·tcp/ip
程序员小寒35 分钟前
前端高频面试题之CSS篇(一)
前端·css·面试·css3
颜酱1 小时前
Monorepo 架构以及工具选型、搭建
前端·javascript·node.js
oden1 小时前
ChatGPT不推荐你?7个GEO技巧让AI主动引用你的内容
前端
李游Leo2 小时前
前端安全攻防指南:XSS / CSRF / 点击劫持与常见防护实践(含真实案例拆解)
前端·安全·xss
我命由我123452 小时前
微信开发者工具 - 模拟器分离窗口与关闭分离窗口
前端·javascript·学习·微信小程序·前端框架·html·js
E***q5392 小时前
Vue增强现实开发
前端·vue.js·ar
S***42802 小时前
JavaScript在Web中的Angular
前端·javascript·angular.js
黑幕困兽2 小时前
ehcarts 实现 饼图扇区间隙+透明外描边
前端·echarts
San302 小时前
深入理解 JavaScript 词法作用域链:从代码到底层实现机制
前端·javascript·ecmascript 6