asp.net限时发送手机验证码

html代码

<p>
                        <strong>手机验证码:</strong> <asp:TextBox ID="code" runat="server" CssClass="box" style="width:90px" maxlength="4"></asp:TextBox> <input type="button" id="btncode" class="diarnu_net_btn" value="发送验证码" /> </p>

提交的时候只需验证Session["phoneCode"]和code.text的值就行

js代码

复制代码
var secs=100;
$("#btncode").click(function(){
    var _phone='18000055568'; this.value=" 正在发送中 "; this.disabled=true; $.post("ajax.aspx",{type:"sendCode",phone:_phone},function(data){ if(data=="1") { //成功 for(var i=0;i<=secs;i++) { window.setTimeout("update(" + i + ")", i*1000); } }else{ alert("验证码发送失败,请检查手机是否输入有误!"); document.getElementById("btncode").value=" 发送验证码 "; document.getElementById("btncode").disabled=false; } }) }) function update(num) { if(num == secs) { document.getElementById("btncode").disabled=false; document.getElementById("btncode").click(); } else { printnr = secs-num; document.getElementById("btncode").value=" "+printnr+"秒后重发 "; } } 
复制代码

ajax.aspx代码

复制代码
using System;
using System.Data;
using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Collections.Generic; public partial class ajax_getdata : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { switch (Request.Form["type"]) { case "sendCode": sendCode(); break; default: Response.Write(""); break; } } } private void sendCode() {string to = Request.Form["phone"]; string ret = null; CCPRestSDK.CCPRestSDK api = new CCPRestSDK.CCPRestSDK(); //ip格式如下,不带https:// bool isInit = api.init("app.cloopen.com", "8883");//sandboxapp.cloopen.com api.setAccount("8a216da85624qwweriov157nfjdjkufd", "7bb627897efndfd54f6");//主账号,主账号令牌 api.setAppId("8a216dueiu438957efjdsfu");//AppID try { if (isInit) { string strcode = new Random().Next(1000, 9999).ToString(); if (MyCLib.StrClass.GetSession("phoneCode") != "") { strcode = MyCLib.StrClass.GetSession("phoneCode"); } Session["phoneCode"] = strcode; string[] data = { strcode, "20" };//验证码,分钟 Dictionary<string, object> retData = api.SendTemplateSMS(to, "1", data);//短信接收号码, 短信模板id, 内容数据 string statusCode = retData["statusCode"].ToString();//statusCode为"000000"表示请求发送成功。statusCode不是"000000",表示请求发送失败 string statusMsg = retData["statusMsg"].ToString(); if (statusCode == "000000") { ret = "1"; } else { ret = statusMsg; } } else { ret = "初始化失败"; } } catch (Exception exc) { ret = exc.Message; } finally { Response.Write(ret); } } }
复制代码

猜你喜欢

转载自www.cnblogs.com/cuihongyu3503319/p/9779677.html