jQuery-获取验证码

1.案例

获取验证码选用模拟一号店登录页面。其有css、HTML、js三部分组成。

2.效果图

1.页面初始效果图

 2.点击获取验证码时效果图

 3.读秒结束后的效果图

 

3.实现思路

其中的表单验证在这里我们用H5验证。

准备:创建触发条件,点击同意协议并注册按钮触发。(即点击事件)

第一步:通过选择器(getElementById)找到相应的input框赋给自己声明的变量A。

第二步:用if判断,条件为(A.validity.valueMissing===true);意思是判断A是否为空,空为true反之false;然后提示信息(setCustomValidity)返回不能为空,第二个条件为(A.validity.patternMissing===true);意思是判断正则表达式是否符合,符合为true反之false;然后提示信息(setCustomValidity)返回错误。如果两者都为false则正确即提示信息(setCustomValidity)返回空(即通过)。

第三步:确认密码:获取两个input的值判断即可;

点击获取按钮后,前方input赋上四位验证码并开始读秒一分钟,一分钟后按钮文本变为重新发送

其中读秒时按钮禁用即点击无效。

第一步:声明秒数初始值a为一分钟即60秒及定时函数b。

第二步:创键获取验证码的点击事件。

第三步:先声明一个字符串c在用循环赋给a四位的随机数最后将c赋给前方的验证码input框。

第四步:先封装一个函数d在其里面写验证码的读秒功能。

第五步:定义定时函数b,调用函数d每1000毫秒执行一次。

第六步:函数d为:a自减一 并判断如果a大于0则一直改变它的值并禁止点击按钮。否则清除禁止、初始值重新为60并且值改变为重新发送并清除定时函数。

4.代码

注:css与js皆是外部引入

1.CSS

*{
    margin: 0;
    padding: 0;
}
a{
    text-decoration: none;
    color: gray;
}
/*头部*/
.header_zhu{
    padding: 15px;
    border-bottom: 1px solid #5a525f;
    box-shadow: 1px 2px 15px 4px  #8080806e;
}
.header_tui{
    display: inline-block;
    margin-left: 970px;
}
.header_zhu img{
    display: inline-block;
    vertical-align: middle;
}
.header_zhu p{
    display: inline-block;
    font-size: 13px;
    vertical-align: middle;
    height: 70px;
    line-height: 70px;
}
.header_zhu p a{
    color: dodgerblue;
}
.header_right{
    display: inline-block;
    border: 1px solid gray;
    position: relative;
    top: 3px;
}
.header_right span a{
    color: black;
}
/*主体*/
section{
   text-align: center;
}
.The_title{
    padding: 30px 0 20px 0;
}
.input_1{
    width: 252px;
    height: 45px;
    line-height: 45px;
    padding-left: 15px;
    margin: 5px 0;
}
span{
    font-size: 12px;
    margin-right: 40px;
    display: inline-block;
    margin-bottom: 5px;
    color: gray;
}
span a{
    color: dodgerblue;
}
.input_2{
    width: 146px;
    height: 45px;
    line-height: 45px;
    padding-left: 15px;
    margin: 5px 0;
}
.input_3{
    width: 101px;
    height: 48px;
    line-height: 48px;
    background: #56565e;
    border: 0;
    color: white;
    font-size: 12px;
    font-weight: bold;
}
.input_4{
    width: 272px;
    height: 42px;
    line-height: 42px;
    padding-left: 15px;
    margin: 5px 0;
    font-size: 13px;
    color: white;
    background: #ff3c3c;
    border: none;
}
/*尾部*/
.footer_first p a{
    color: rgba(0, 0, 0, 0.65);
    font-size: 12px;
}
.footer_first{
    padding-top: 12px;
    text-align: center;
}

2.HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册</title>
    <link rel="stylesheet" href="css/registered.css">
    <script src="js/jquery-1.12.4.js"></script>
    <script src="JS外部/registered.js"></script>
</head>
<body>
<header>
    <div class="header_zhu">
        <img src="images/loginlogo.jpg" alt="">
        <div class="header_tui">
            <p>你好!欢迎光临1号店 <a href="#">请登录</a></p>
            <div class="header_right">
                <a href="#"><img src="images/runbun.png" alt=""></a>
                <span><a href="#">帮助中心</a></span>
                <a href="#"><img src="images/turnb.png" alt=""></a>
            </div>
        </div>
    </div>
</header>
<section>
    <div class="section_first">
        <h1 class="The_title">一号店注册</h1>
        <form action="Index.html" method="post">
            <p>
                <input type="text" name="Mobile phone no" required pattern="1\d{10}" placeholder="手机号"
                       class="input_1" id="phone">
            </p>
            <p>
                <input type="text" name="Mobile phone no——yanZhEng" placeholder="验证码" class="input_2">
                <input type="button" name="Verification code" value="获取验证码" class="input_3">
            </p>
            <p>
                <input type="password" name="password" required  class="input_1" placeholder="设置密码" id="pass1" pattern="\d{6,10}">
            </p>
            <p>
                <input type="password" name="password-a" placeholder="确认密码" class="input_1" id="pass2">
            </p>
            <span>
                    点击注册,表示您同意1号店<a href="#">《服务协议》</a>
                </span>
            <p>
                <input type="submit" value="同意协议并注册" name="tiJiaO" class="input_4">
            </p>
        </form>
    </div>
</section>
<footer>
    <div class="footer_first">
        <p><a href="#">沪ICP备13044278号|</a><a href="#">合字B1.B2-20130004|</a><a href="#">营业执照</a></p>
        <p><a href="#">Copyrightc1号店网上超市2007-2016,All Rights Reserved</a></p>
    </div>
</footer>
</body>
</html>

3.JS 注:jQuery库用的是1.12.4版本的。

$(document).ready(function () {
    $(".input_4").click(function () {
        //手机号
        var phone = document.getElementById("phone");
        if (phone.validity.valueMissing===true){
            phone.setCustomValidity("手机号不能为空!");
        } else if (phone.validity.patternMismatch===true){
            phone.setCustomValidity("手机号错误!");
        }else{
            phone.setCustomValidity("");
        }
        //设置密码
        var pass = document.getElementById("pass1");
        if (pass.validity.valueMissing === true){
            pass.setCustomValidity("密码不能为空!");
        }else if (pass.validity.patternMismatch === true){
            pass.setCustomValidity("密码错误!");
        }else{
            pass.setCustomValidity("");
        }
        //确认密码
        mia();//调用方法
        //封装成方法
        function mia() {
            var number1 = document.getElementById("pass1");
            var number2 = document.getElementById("pass2");
            if (number1.value!==number2.value){
                number2.setCustomValidity("密码不一致!");
            }else{
                number2.setCustomValidity("");
            }
        }
    });
    //验证码
    var minth = 60;//秒数初始值
    var mytime;//定时函数
    $(".input_3").click(function () {
        //点击事件
        var  str;
        str="";
        for (var i = 0; i < 4; i++) {
            var j = Math.floor(Math.random()*10);//向下取整
            str+=j;//累加
        }
        $(".input_2").val(str);
        //文本
        mytime = setInterval(fn,1000);
        //封装一个倒计时方法
        function fn() {
            minth = --minth;
            if (minth>0){//还没有倒计时结束
                $(".input_3").val('('+minth+'秒)后重发').attr("disabled","disabled");
            }else{//倒计时结束
                $(".input_3").removeAttr("disabled");
                minth = 60;//重新初始化
                $(".input_3").val('重新发送');
                clearInterval(mytime);//清除定时函数
            }
        }
    });
});

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值