js H5驗證碼倒計時
阿新 • • 發佈:2021-02-09
驗證碼倒計時
直接上html
<ul class="ulist">
<li class="group">
<div class="controls">
<input type="text" id="phone" class="txt" placeholder="輸入手機號">
</div>
< /li>
<li class="group">
<div class="controls">
<input type="text" id="code" class="txt" placeholder="輸入驗證碼">
<button class="btn btn-small get-code" onclick="getCode(this)" id="J_getCode">獲取驗證碼</button>
<button class="btn btn-small reset-code" id="J_resetCode" style="display:none;"><span id="J_second">60</span>秒後重發</button>
</div>
</li>
</ul>
js
<script>
/*獲取驗證碼*/
var isPhone = 1;
function getCode(e) {
checkPhone(); //驗證手機號碼 if (isPhone) {
resetCode(); //倒計時
} else {
$('#phone').focus();
}
}
//驗證手機號碼
function checkPhone() {
var phone = $('#phone').val();
var pattern = /^1[0-9]{10}$/;
isPhone = 1;
if (phone == '') {
alert('請輸入手機號碼');
isPhone = 0;
return;
}
if (!pattern.test(phone)) {
alert('請輸入正確的手機號碼');
isPhone = 0;
return;
}
}
//倒計時
function resetCode() {
$('#J_getCode').hide();
$('#J_second').html('60');
$('#J_resetCode').show();
var second = 60;
var timer = null;
timer = setInterval(function() {
second -= 1;
if (second > 0) {
$('#J_second').html(second);
} else {
clearInterval(timer);
$('#J_getCode').show();
$('#J_resetCode').hide();
}
}, 1000);
}
</script>