vue实现动态验证码和短信验证码

一:动态验证码:

html部分

<div style="display: flex;align-items: center;">
                    <span>验证码:</span>
                    <i-input v-model="picLyanzhengma" placeholder="请输入验证码" style="width: 300px" @on-blur="checkLpicma"></i-input>
                    <input type="button" id="code" @click="createCode" v-model="checkCode"/ class="yanzhengma_inp">

                </div>

js部分:

// 图片验证码
    createCode(){
      code = ""; 
      var codeLength = 4;//验证码的长度 
      var random = new Array(0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R', 
         'S','T','U','V','W','X','Y','Z');
      for(var i = 0; i < codeLength; i++) {
       var index = Math.floor(Math.random()*36);//取得随机数的索引(0~35) 
       code += random[index];//根据索引取得随机数加到code上 
      } 
      this.checkCode = code;//把code值赋给验证码 
    },
    // 验证所输入验证码是否一致,不区分大小写
    checkLpicma(){   
      this.picLyanzhengma.toUpperCase();//取得输入的验证码并转化为大写   
      if(this.picLyanzhengma == '') {
       this.$Message.info('请输入验证码'); 
      }else if(this.picLyanzhengma.toUpperCase() != this.checkCode ) { 
       this.$Message.info('验证码输入错误'); 
       this.createCode();//刷新验证码 
       this.picLyanzhengma = '';
      }else {
       //输入正确时 
      } 

    }

二:短信验证码:

html部分

<div>
                    <span>短信验证码:</span>
                    <i-input v-model="registration_dx_yzm" placeholder="请输入短信验证码" style="width: 300px"></i-input>
                    <i-button type="primary" @click="dx_yzm_bt" style="width: 120px;margin-left: 12px;" v-show="is_dx_yz">获取验证码
                    </i-button> 
                    <i-button type="primary" style="width: 120px;margin-left: 12px;" v-show="!is_dx_yz">{{time}}秒后重新获取
                    </i-button>   

                </div>

js部分

 // 短信验证码函数
    dx_yzm_bt(){
      this.is_dx_yz=false
      let dx_djs=setInterval(()=>{
        if ((this.time--) <= 0) {
          this.time = 60;
          this.is_dx_yz = true;
          clearInterval(dx_djs);
        }
      },1000)
    },

猜你喜欢

转载自blog.csdn.net/wangle_style/article/details/80453529