Date
Sep. 8th, 2024
 
2024年 8月 6日

Post: JavaScript: 短信验证码

JavaScript: 短信验证码

Published 12:07 Jul 08, 2013.

Created by @ezra. Categorized in #Programming, and tagged as #JavaScript.

Source format: Markdown

Table of Content

相信大家都遇到过手机验证码的功能,今天介绍一个简单的实现方式。

先做一个简单页面:

手机号:

验证码:

// 发送按钮方法
$(document).on('click', "#getCheckId", function(e) {
  time(this);
  $.ajax({
    url: "checkPhoneNumber/sendCheckNumber",
    data: {
      "operatorTel": $("#operatorTel").val()
    },
    async: false,
    dataType: "text",
    success: function(res) {
      if (data == 'true') {
        alert("验证码发送成功");
      } else {
        alert("验证码发送失败");
      }
    },
    error: function(xhr) {
      alert("请求出错");
    }
  });
});
// 倒计时方法
var wait = 60;
function time(obj) {
  if (wait == 0) {
    $("#getCheckId").removeAttr("disabled");
    $("#getCheckId").val("获取验证码");
    wait = 60;
  } else {
     $("#getCheckId").attr("disabled", "true");
     $("#getCheckId").val(wait + "秒后重试");
     wait--;
     setTimeout(function() {
       time(obj);
     }, 1000)
  }
}
// 校验验证码方法
function check() {
  $.ajax({
    url: "checkPhoneNumber/check",
    data: {
      "checkNumber": $("#checkNumber").val();
      "operatorTel": $("#operatorTel").val()
    },
    async: false,
    dataType: "text",
    success: function(res) {
      if (data == 'true') {
        alert("验证码发送成功");
        time(this)
      } else {
        alert("验证码发送失败");
      }
    },
    error: function(xhr) {
      alert("请求出错");
    }
  });
}
Pinned Message
HOTODOGO
The Founder and CEO of Infeca Technology.
Developer, Designer, Blogger.
Big fan of Apple, Love of colour.
Feel free to contact me.
反曲点科技创始人和首席执行官。
程序猿、设计师、奇怪的博主。
苹果死忠、热爱色彩斑斓的世界。
如有意向请随时 与我联系