手机号验证码重新发送

前文叙述

很久以前做的一个 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>
相关推荐
咖啡の猫2 分钟前
Python集合生成式
前端·javascript·python
lcc1878 分钟前
CSS3 新增文本属性
css3
QT 小鲜肉12 分钟前
【Linux命令大全】001.文件管理之mtoolstest命令(实操篇)
linux·运维·前端·笔记·microsoft
holeer14 分钟前
React UI组件封装实战——以经典项目「个人博客」与「仿手机QQ」为例
前端·javascript·react.js·ui·前端框架·软件工程
chilavert31821 分钟前
技术演进中的开发沉思-277 AJax :Calendar
前端·javascript·microsoft·ajax
debug 小菜鸟22 分钟前
搭建web 环境的那些事
前端
鹏多多24 分钟前
Flutter下拉刷新上拉加载侧拉刷新插件:easy_refresh全面使用指南
android·前端·ios
Mike_jia37 分钟前
OpenDeRisk:AI 原生风险智能系统 ——7*24H 应用系统AI数字运维助手(AI-SRE)
前端
朱穆朗1 小时前
electron升级到33.0.x版本后,devtools字体的修改方法
前端·javascript·electron
IT_陈寒1 小时前
Java 21新特性实战:5个必学的性能优化技巧让你的应用提速40%
前端·人工智能·后端