jQuery实现倒计时重新发送短信验证码功能示例
本文实例讲述了jQuery实现倒计时重新发送短信验证码功能的方法。分享给大家供大家参考,具体如下:
<!DOCTYPEhtml>
<html>
<head>
<scriptsrc="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script>
<scripttype="text/javascript">
varcountdown=60;
functionsettime(obj){
if(countdown==0){
obj.removeAttribute("disabled");
obj.value="免费获取验证码";
countdown=60;
return;
}else{
obj.setAttribute("disabled",true);
obj.value="重新发送("+countdown+")";
countdown--;
}
setTimeout(function(){
settime(obj)}
,1000)
}
</script>
<body>
<inputtype="button"id="btn"value="免费获取验证码"onclick="settime(this)"/>
</body>
</html>
实践例子:
<!DOCTYPEhtml>
<html>
<head>
<metacharset="UTF-8">
<metaname="viewport"content="width=device-width,initial-scale=1">
<title>抢占iPhone7专属预订名额</title>
<linkrel="stylesheet"href="getiphone.css"/>
</head>
<bodystyle="width:100%;height:100%;background:#fffurl(iponeImg/beijing.png)no-repeatcenterfixed;background-size:100%100%;text-align:center;">
<divclass="top">
<imgsrc="iponeImg/wenan.png"/>
</div>
<h1class="title"><span></span>上市时间同步发售<span></span><br/>首批新苹果30个预订专属名额</h1>
<formid="activityForm"class="form-horizontal"method="post"action="/wamei/activityController/save.htm">
<label>
<span><imgsrc="iponeImg/yonghu.png"/></span>
<i></i>
<inputtype="text"class="inline-input"id="name"name="name"placeholder="请输入您的真实姓名"datatype="*1-20"errormsg="姓名最多20个中英文字符!"nullmsg="请输入您的真实姓名!"/>
</label>
<label>
<span><imgsrc="iponeImg/dianhua.png"/></span>
<i></i>
<inputtype="text"class="inline-input"id="mobilePhone"name="mobilePhone"placeholder="请输入您的手机号码"datatype="*"errormsg="请输入您的手机号码!"nullmsg="请输入您的手机号码!"/>
</label>
<label>
<span><imgsrc="iponeImg/yanzhengma.png"/></span>
<i></i>
<inputtype="text"id="validateCode"class="inline-input"name="validateCode"placeholder="验证码"datatype="*"errormsg="请输入验证码!"nullmsg="请输入验证码!"style="width:180px;"/>
<aid="num"mark="1">获取验证码</a>
</label>
<labelid="get">
<inputid="confirm"type="submit"value="提交抢占名额"/>
</label>
<label>
<ahref="/wamei/pages/activity/details.html"id="activity">查看活动详情</a>
</label>
</form>
<divclass="bottom">
<span></span>
<imgsrc="iponeImg/logo.png"/>
<span></span>
</div>
<divid="success">
<divclass="successImg">
<aclass="close"></a>
<p><imgsrc="iponeImg/chenggong.png"/>恭喜提交成功</p>
<pstyle="padding-top:0px;font-size:23px;">分享一下,安抚激动的心</p>
</div>
</div>
</body>
</html>
<!--scripts-->
<scriptsrc="/wamei/pages/js/jquery.min.js"></script>
<scriptsrc="/wamei/pages/js/jquery.form.js"></script>
<scriptsrc="/wamei/pages/js/bootstrap.min.js"></script>
<scriptsrc="/wamei/pages/js/Validform_v5.3.2.js"></script>
<scripttype="text/javascript">
$(function(){
//提交表单
$("#activityForm").Validform({
btnSubmit:"#confirm",
tiptype:function(msg){
if(msg!=''&&msg!='通过信息验证!'){
alert(msg);
}
},
tipSweep:true,
beforeSubmit:function(){
saveForm();
returnfalse;
}
});
});
//只允许在APP内打开
functionisMobile(){
varu=navigator.userAgent;
varmobileFlag=u.indexOf('type/tfbrowser')>-1;
returnmobileFlag;
}
//手机号码验证
$("#mobilePhone").blur(function(){
varmobilePhone=$("#mobilePhone").val();
varmyreg=/^(((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("请输入有效的手机号码!");
$("#mobilePhone").val("");
return;
}
}
});
//发送短信验证码
$("#num").click(function(){
varmobilePhone=$("#mobilePhone").val();
varurl="/wamei/activityController/sendValidCode.htm";
if(!mobilePhone){
alert("请输入您的手机号码!");
return;
}
varmark=$("#num").attr("mark");
if("1"==mark){
settime(this);
$.post(url,{mobilePhone:mobilePhone},function(html){
vardata=eval("("+html+")");
if(data&&data.statusCode==1){
console.log("sendsuccess!");
alert("短信验证码已发送,请查收!");
}else{
alert(data.msg);
}
});
}
});
//提交form表单
functionsaveForm(){
$("#activityForm").ajaxSubmit({
//data:{'columnStr':columnStr},
type:'post',
async:false,
success:function($data){
vardata=eval("("+$data+")");
if(data&&data.statusCode==1){
$("#success").show();
}else{
alert(data.msg);
$("#mobilePhone").val("");
}
}
});
}
$(".close").click(function(){
$("#success").hide();
window.location.reload();
})
//短信后倒计时
varcountdown=60;
functionsettime(obj){
if(countdown==0){
$(obj).attr("disabled",false);
$(obj).attr("mark","1");
$(obj).html("获取验证码");
countdown=60;
return;
}else{
$(obj).attr("disabled",true);
$(obj).attr("mark","0");
$(obj).html("重新发送("+countdown+")");
countdown--;
}
setTimeout(function(){
settime(obj)}
,1000)
}
//$("#confirm").click(function(){
//$("#success").show();
//})
</script>
PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:
JavaScript正则表达式在线测试工具:
http://tools.jb51.net/regex/javascript
正则表达式在线生成工具:
http://tools.jb51.net/regex/create_reg
更多关于jQuery相关内容感兴趣的读者可查看本站专题:《jQuery日期与时间操作技巧总结》、《jQuery扩展技巧总结》、《jQuery常用插件及用法总结》、《jQuery表格(table)操作技巧汇总》、《jquery中Ajax用法总结》及《jquery选择器用法总结》
希望本文所述对大家jQuery程序设计有所帮助。