javascript发送验证码

时间:2022-12-30 19:20:19

一个发送验证码的javascript代码

var T = 0;
var handle = null;
var event = null;
$(
function(){
$(
'#sendCode').click(function(e){
if(T == 0) {
var mobile = $('#name').val();
if (!/^(13[0-9]|14[0-9]|15[0-9]|18[0-9])\d{8}$/i.test(mobile)) {
alert(
'请先填写正确的手机号码');
return false;
}

$.post(
'/auth/sendcode', {mobile: mobile, _token: 'HyOSLkoOpvawVZZi6LUYfNIryJZvc2mXnOJYrrDJ'}, function (res) {
if (res.state == 'success') {
T
= 59;
countDown();
}
else {
alert(res.message);
}
},
'json');

}
else {
e.stopPropagation();
}
});
});
function countDown()
{
$(
'#sendCode').text(T+'秒后可重新获取').css({'background-color':'#f8f8f8', 'color' : '#cccccc'});
T
= T - 1;
handle
= setTimeout('countDown()', 1000);
if (T < 0) {
clearTimeout(handle);
$(
'#sendCode').text('重新获取').css({'background-color':'#eeeeee', 'color' : '#555555'});
T
= 0;
}
}