<input type="button" id="btn" value="获取验证码" onclick="sendyzm();aaa(this)" />
<script type="text/javascript">
function sendyzm() {
var phone = document.getElementById("phone").value;
var tel = document.all("phone").value;
var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
if(phone == '') {
alert('请填写手机号')
} else {
if(myreg.test($("#phone").val())) {
var num = "";
for(var i = 0; i < 6; i++) {
num += Math.floor(Math.random() * 10)
}
$.ajax({
type: 'post',
data: {
'phone': phone,
'yzm': num
},
url: "短信接口",
dataType: "json",
success: function(json) {
$.ajax({
type: 'post',
data: {
'phone': phone,
'yzmm': num
},
url: "{php echo $this->createMobileUrl('XXX');}",
dataType: "json",
success: function(json) {}
});
}
});
alert('已发送至您的手机');
} else {
alert("手机号错误");
}
}
}
var countdown = 60;
function aaa(obj) {
var phone = document.getElementById("phone").value;
var tel = document.all("phone").value;
var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
if(myreg.test($("#phone").val())) {
if(countdown == 0) {
obj.removeAttribute("disabled");
obj.value = "获取验证码";
countdown = 60;
return;
} else {
obj.setAttribute("disabled", true);
obj.value = "重新发送(" + countdown + ")";
countdown--;
}
setTimeout(function() {
aaa(obj)
}, 1000);
} else {
return false;
}
}
</script>