js实现通过手机号发送验证码功能

在这里插入图片描述
代码如下:

<!DOCTYPE html>
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>发送验证码</title>
<style>
.contain{
width:300px;
height:200px;
margin:100px auto;
background-color:blue;
}
#phone{
width:190px;
margin-top:60px;
}
#verCode{
width:103px;
}
</style>
</head>
<body>
<div class="contain" >
手机号:<input type="text" name="phone" id="phone"  placeholder="请输入手机号" maxlength="11" /><br /><br />
验证码:<input type="" name="verCode" id="verCode"  placeholder="请输入验证码" maxlength="6"/>
<input type="button" name="" id="verCodeBtn" value="获取验证码" onclick="settime(this);"/>
</div>

<script src="http://www.lanrenzhijia.com/ajaxjs/jquery.min.js"></script>
<script>
//验证码
var counts = 60;
function settime(val) {
 if(counts == 0) {
  val.removeAttribute("disabled");
  val.value = "获取验证码";
  counts = 60;
  return false;
 } else {
  val.setAttribute("disabled", true);
  val.value = "重新发送(" + counts + ")";
  counts--;
 }
 setTimeout(function() {
  settime(val);
 }, 1000);
}

 //获取验证码
$(function(){
 $("#verCodeBtn").click(function() {
  var userinfo = {
   "UserPhoneNum": '86//' + $("input[name='phone']").val()
  }
  $.ajax({
   url: "https://www.xxxxx.cn/user/sendcode/",
   data: userinfo,
   type: "get",
   success: function(data) {
    if(JSON.parse(data).state === 404 || JSON.parse(data).state === 202 || userinfo.UserPhoneNum === '86//') {
     alert("验证码发送失败")
    } else {
     alert("验证码发送成功,请耐心等待")
    }
   },
   error: function() {
    alert("发送失败");
   }
  });
 });
})
</script>
</body>
<

/html>

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
以下是一个简单的示例代码,用于使用HTML+CSS+JS实现发送验证码功能: HTML代码: ```html <form> <label for="phone">手机号:</label> <input type="tel" id="phone" name="phone" required> <button type="button" id="send-code">发送验证码</button> <br> <label for="code">验证码:</label> <input type="text" id="code" name="code" required> <button type="submit">提交</button> </form> ``` CSS代码: ```css form { display: flex; flex-direction: column; align-items: center; } input[type="tel"] { padding: 8px; margin: 8px; width: 200px; border-radius: 4px; border: 1px solid #ccc; } button { padding: 8px; margin: 8px; border-radius: 4px; border: none; cursor: pointer; } #send-code { background-color: #4CAF50; color: white; } #send-code:disabled { background-color: #ccc; cursor: not-allowed; } #code { width: 100px; } ``` JS代码: ```js const phoneInput = document.getElementById('phone'); const sendCodeButton = document.getElementById('send-code'); const codeInput = document.getElementById('code'); let countdown = 60; let countdownTimer; function startCountdown() { countdownTimer = setInterval(() => { countdown--; sendCodeButton.textContent = `重新发送 (${countdown}s)`; if (countdown === 0) { clearInterval(countdownTimer); sendCodeButton.textContent = '发送验证码'; sendCodeButton.disabled = false; countdown = 60; } }, 1000); } sendCodeButton.addEventListener('click', () => { // TODO: 发送验证码 sendCodeButton.disabled = true; startCountdown(); }); document.querySelector('form').addEventListener('submit', (event) => { event.preventDefault(); // TODO: 提交验证码 }); ``` 在上面的代码中,我们使用了`input`元素的`type="tel"`属性来限制用户只能输入手机号码。我们还设置了一个计时器来处理重新发送验证码的倒计时。当用户点击发送验证码按钮时,我们会禁用该按钮并开始计时器。当倒计时结束时,我们会重置按钮状态并重新启用该按钮。在表单提交时,我们可以使用相关的代码来验证和提交验证码。注意,这个示例只是一个基本的框架,具体实现可能因需求而异。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值