验证手机号和设置60秒的倒计时
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<title></title>
</head>
<body>
<div class="body">
<ul>
<li>
<span class="font">
手机号码
</span>
<input type="number" name="phone" id="phone" value="" placeholder="手机号码"/>
</li>
<li>
<span class="font">
验证码
</span>
<!--<input type="number" name="" id="code" value="" maxlength="6" placeholder="登录验证码"/>-->
<input type="button" class="btn" value="获取登录验证码">
</li>
</ul>
</div>
</body>
<script src="js/jquery-2.1.4.js" type="text/javascript" charset="utf-8"></script>
<script>
$(function(){
$('#phone').bind('focus',function(){
$(this).css('color','black');
})
//限制手机号码的长度
$('#phone').bind('input',function(){
var phonelength = $(this).val().length;
if(phonelength >11){
var phone = $(this).val().substr(0,11);
$(this).val(phone);
}
})
//手机号码的正则判断
var phoneflag;
$('#phone').bind('blur',function(){
var phone = $(this).val();
if(!/^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}|(170[059])[0-9]{6}$/i.test(phone))
{
$('#phone').css('color','red');
phoneflag=0;
}else if(/^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}|(170[059])[0-9]{6}$/i.test(phone)){
phoneflag=1;
}
});
//获取登录验证码的倒计时状态
$('.btn').bind('click',function(){
var codeflag = 60;
if(phoneflag == 1&&codeflag==60){
var timeflag = setInterval(function(){
if(codeflag>0){
$('.btn').val(codeflag+'秒后重发');
$('.btn').css({'color':'gray'});
$('.btn').attr('disabled','disabled');
codeflag--;
}else {
codeflag=60;
$('.btn').val('重新发送验证码');
$('.btn').css({'color':'#0000FF'});
$('.btn').removeAttr('disabled');
clearInterval(timeflag);
}
},1000);
}
})
})
</script>
</html>