zoukankan      html  css  js  c++  java
  • asp.net限时发送手机验证码

    html代码

    <p>
                            <strong>手机验证码:</strong>
                            <asp:TextBox ID="code" runat="server" CssClass="box" style="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);
            }
        }
    }
    复制代码
  • 相关阅读:
    奇怪的html控件textarea
    ado.net快速上手实践篇(二)
    巧用apply让javascript函数仅执行一次
    javascript:像操作Array一样操作NodeList
    javascript下的数值型比较真的没有那么简单
    ado.net快速上手实践篇(一)
    ado.net快速上手疑问及解答(完结篇)
    如何利用【百度地图API】进行定位?非GPS定位
    【百度地图API】关于如何进行城市切换的三种方式
    【百度地图API】建立全国银行位置查询系统(四)——如何利用百度地图的数据生成自己的标注
  • 原文地址:https://www.cnblogs.com/cuihongyu3503319/p/9779677.html
Copyright © 2011-2022 走看看