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("8a216da85624qwwedriov157nfjdjkufd", "7bb62789d7efndfd54f6");//主賬號,主賬號令牌 api.setAppId("8a216dueiu4389d57efjdsfu");//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); } } }
發送手機驗證碼方法請看我上一篇博文http://www.cnblogs.com/webapi/p/5711764.html
