jQuery實(shí)現(xiàn)倒計(jì)時(shí)重新發(fā)送短信驗(yàn)證碼功能示例
本文實(shí)例講述了jQuery實(shí)現(xiàn)倒計(jì)時(shí)重新發(fā)送短信驗(yàn)證碼功能的方法。分享給大家供大家參考,具體如下:
<!DOCTYPE html> <html> <head> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script> <script type="text/javascript"> var countdown=60; function settime(obj) { if (countdown == 0) { obj.removeAttribute("disabled"); obj.value="免費(fèi)獲取驗(yàn)證碼"; countdown = 60; return; } else { obj.setAttribute("disabled", true); obj.value="重新發(fā)送(" + countdown + ")"; countdown--; } setTimeout(function() { settime(obj) } ,1000) } </script> <body> <input type="button" id="btn" value="免費(fèi)獲取驗(yàn)證碼" onclick="settime(this)" /> </body> </html>
實(shí)踐例子:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>搶占iPhone7專屬預(yù)訂名額</title> <link rel="stylesheet" href="getiphone.css" /> </head> <body style="width: 100%;height: 100%;background:#fff url(iponeImg/beijing.png) no-repeat center fixed;background-size:100% 100%;text-align: center;"> <div class="top"> <img src="iponeImg/wenan.png" /> </div> <h1 class="title"><span></span>上市時(shí)間同步發(fā)售<span></span><br />首批新蘋果30個(gè)預(yù)訂專屬名額</h1> <form id="activityForm" class="form-horizontal" method="post" action="/wamei/activityController/save.htm"> <label> <span><img src="iponeImg/yonghu.png" /></span> <i></i> <input type="text" class="inline-input" id="name" name="name" placeholder="請輸入您的真實(shí)姓名" datatype="*1-20" errormsg="姓名最多20個(gè)中英文字符!" nullmsg="請輸入您的真實(shí)姓名!" /> </label> <label> <span><img src="iponeImg/dianhua.png" /></span> <i></i> <input type="text" class="inline-input" id="mobilePhone" name="mobilePhone" placeholder="請輸入您的手機(jī)號碼" datatype="*" errormsg="請輸入您的手機(jī)號碼!" nullmsg="請輸入您的手機(jī)號碼!" /> </label> <label> <span><img src="iponeImg/yanzhengma.png" /></span> <i></i> <input type="text" id="validateCode" class="inline-input" name="validateCode" placeholder="驗(yàn)證碼" datatype="*" errormsg="請輸入驗(yàn)證碼!" nullmsg="請輸入驗(yàn)證碼!" style="width:180px;" /> <a id="num" mark="1" >獲取驗(yàn)證碼</a> </label> <label id="get"> <input id="confirm" type="submit" value="提交搶占名額" /> </label> <label> <a href="/wamei/pages/activity/details.html" id="activity">查看活動(dòng)詳情</a> </label> </form> <div class="bottom"> <span></span> <img src="iponeImg/logo.png" /> <span></span> </div> <div id="success"> <div class="successImg"> <a class="close"></a> <p><img src="iponeImg/chenggong.png" />恭喜提交成功</p> <p style="padding-top: 0px;font-size: 23px;">分享一下,安撫激動(dòng)的心</p> </div> </div> </body> </html> <!-- scripts --> <script src="/wamei/pages/js/jquery.min.js"></script> <script src="/wamei/pages/js/jquery.form.js"></script> <script src="/wamei/pages/js/bootstrap.min.js"></script> <script src="/wamei/pages/js/Validform_v5.3.2.js"></script> <script type="text/javascript"> $(function(){ //提交表單 $("#activityForm").Validform({ btnSubmit:"#confirm", tiptype:function(msg){ if(msg != '' && msg!='通過信息驗(yàn)證!'){ alert(msg); } }, tipSweep:true, beforeSubmit:function(){ saveForm(); return false; } }); }); //只允許在APP內(nèi)打開 function isMobile(){ var u = navigator.userAgent; var mobileFlag = u.indexOf('type/tfbrowser') > -1; return mobileFlag; } //手機(jī)號碼驗(yàn)證 $("#mobilePhone").blur(function(){ var mobilePhone =$("#mobilePhone").val(); var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(17[0]{1})|(15[0-3]{1})|(15[5-9]{1})|(18[0-9]{1}))+\d{8})$/; if(mobilePhone){ if(!myreg.test(mobilePhone)){ alert("請輸入有效的手機(jī)號碼!"); $("#mobilePhone").val(""); return ; } } }); //發(fā)送短信驗(yàn)證碼 $("#num").click(function(){ var mobilePhone =$("#mobilePhone").val(); var url="/wamei/activityController/sendValidCode.htm"; if(!mobilePhone){ alert("請輸入您的手機(jī)號碼!"); return ; } var mark = $("#num").attr("mark"); if("1"==mark){ settime(this); $.post(url,{mobilePhone:mobilePhone},function(html){ var data = eval("("+html+")"); if(data && data.statusCode==1){ console.log("send success!"); alert("短信驗(yàn)證碼已發(fā)送,請查收!"); }else{ alert(data.msg); } }); } }); //提交form表單 function saveForm(){ $("#activityForm").ajaxSubmit({ //data: {'columnStr':columnStr}, type: 'post', async: false, success: function($data) { var data = eval("("+$data+")"); if(data && data.statusCode==1){ $("#success").show(); }else{ alert(data.msg); $("#mobilePhone").val(""); } } }); } $(".close").click(function(){ $("#success").hide(); window.location.reload(); }) //短信后倒計(jì)時(shí) var countdown=60; function settime(obj) { if (countdown == 0) { $(obj).attr("disabled",false); $(obj).attr("mark","1"); $(obj).html("獲取驗(yàn)證碼"); countdown = 60; return; } else { $(obj).attr("disabled", true); $(obj).attr("mark","0"); $(obj).html("重新發(fā)送(" + countdown + ")"); countdown--; } setTimeout(function() { settime(obj) } ,1000) } // $("#confirm").click(function(){ // $("#success").show(); // }) </script>
PS:這里再為大家提供2款非常方便的正則表達(dá)式工具供大家參考使用:
JavaScript正則表達(dá)式在線測試工具:
http://tools.jb51.net/regex/javascript
正則表達(dá)式在線生成工具:
http://tools.jb51.net/regex/create_reg
更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jQuery日期與時(shí)間操作技巧總結(jié)》、《jQuery擴(kuò)展技巧總結(jié)》、《jQuery常用插件及用法總結(jié)》、《jQuery表格(table)操作技巧匯總》、《jquery中Ajax用法總結(jié)》及《jquery選擇器用法總結(jié)》
希望本文所述對大家jQuery程序設(shè)計(jì)有所幫助。
相關(guān)文章
jquery二級導(dǎo)航內(nèi)容均分的原理及實(shí)現(xiàn)
頭部導(dǎo)航二級導(dǎo)航有些內(nèi)容太長,一列的話太過難看,就要分成兩列,要做到按塊盡量均分,排列順序沒有限制2013-08-08jQuery動(dòng)畫效果相關(guān)方法實(shí)例分析
這篇文章主要介紹了jQuery動(dòng)畫效果相關(guān)方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了jQuery實(shí)現(xiàn)動(dòng)畫效果所用到的常見方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2015-12-12JQUERY 設(shè)置SELECT選中項(xiàng)代碼
本篇文章主要是對JQUERY 設(shè)置SELECT選中項(xiàng)的代碼進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-02-02jQuery中removeData()方法用法實(shí)例
這篇文章主要介紹了jQuery中removeData()方法用法,實(shí)例形式分析了removeData()方法移除匹配元素指定數(shù)據(jù)的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2014-12-12利用jQuery設(shè)計(jì)一個(gè)簡單的web音樂播放器的實(shí)例分享
這篇文章主要介紹了利用jQuery設(shè)計(jì)一個(gè)簡單的web音樂播放器的實(shí)例分享,包括PHP后臺(tái)和從MySQL中讀取曲目等基本功能的實(shí)現(xiàn),需要的朋友可以參考下2016-03-03