JS发送短信验证码

时间:2023-11-27 23:43:08
<div>
<input type="tel" id="mobile" name="mobile" placeholder="输入手机号码" />
<button onclick="getSms();return false" id="yzmMsg">获取验证码</button>
</div>
<div>
<input type="text" id="captcha" name="captcha" placeholder="输入短信验证码" />
</div>
var i = 60;
var smsback = "";
function getSms(){
var mobileReg = /^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\d{8}$/;
var mobile = $("#mobile").val();
if(!mobileReg.test(mobile)){
layer.msg("请填写正确的手机号码");
return;
}
if(i==60){
changeMsg();
}else{
return;
}
$.ajax({
url:"${base}/sendMsg.jspx",
data:{
"mobile":mobile
}
dataType:"json",
type:"POST",
success:function (data) {
smsback = data.msgCode;
alert(data.testCode);
}
});
} function changeMsg(){
if(i>0){
$("#yzmMsg").html(i+"秒后再次发送");
i--;
setTimeout('changeMsg()',1000);
}else{
$("#yzmMsg").html("获取验证码");
i=60;
} }

  

JAVA调用阿里云短信接口:https://www.cnblogs.com/pxblog/p/11982524.html