js 验证码 倒计时60秒

JS代码

    <script type="text/javascript">
        $(function () {
            var countdown = 60;

            function settime() {
                if (countdown == 0) {
                    $('.getYZM').html("获取验证码");
                    countdown = 60;
                    return false;
                } else {
                    $('.getYZM').html("重新获取倒计时:" + countdown + "s");
                    countdown--;
                }
                setTimeout(function () {
                    settime();
                }, 1000);
            }

            $('.getYZM').click(function () {
                var phone = $('input[name="phone"]').val();
                if (phone == "") {
                    layer.msg('请输入手机号码');
                    return;
                }
                if (!checkMobile(phone)) {
                    layer.msg('请输入正确手机号码');
                    return;
                }

                var thisVal = $(this).html();

                console.log(thisVal);
                if (thisVal == "获取验证码") {
                    settime();
                    $.ajax({
                        url: "../ajax.aspx?act=GetIdentifyingcode&s=" + phone + ",1",
                        type: "post",
                        success: function (data) {
                            layer.msg(data);
                            console.log(data);
                        }
                    });
                }
            });

            function checkMobile(phone) {
                if (!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(phone))) {
                    console.log('false');
                    $('input[name="phone"]').focus();
                    return false;
                } else {
                    return true;
                }
            }
        });
    </script>

 最终效果

 

posted @ 2017-11-17 15:08  大番薯没有心  阅读(915)  评论(0)    收藏  举报