Date
May. 20th, 2024
 
2024年 4月 13日

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
I'm looking for a SOFTWARE PROJECT DIRECTOR / SOFTWARE R&D DIRECTOR position in a fresh and dynamic company. I would like to gain the right experience and extend my skills while working in great teams and big projects.
Feel free to contact me.
For more information, please view online résumé or download PDF
本人正在寻求任职 软件项目经理 / 软件技术经理 岗位的机会, 希望加⼊某个新鲜⽽充满活⼒的公司。
如有意向请随时 与我联系
更多信息请 查阅在线简历下载 PDF