登录(实时验证手机格式、验证码)

思路:
动态验证手机号格式:
	利用计算属性会根据内容的变化而调用的特点,将表单绑定v-model,在
	计算属性中,利用正则不断检验手机格式,正确返回true
	
验证码:
	当手机号格式正确时,点击才有效,利用时间制造三元运算符改变验证码的文本内容,且倒计时期间,再次点击无效

代码示例:

<template>
  <div class='login'>
    <p @click='$router.back()'><</p>

    <div class='md'>
      <p>Jeff外卖</p>
      <div class='linfo'>
      
      //验证手机号
    <div v-show='flag'>
      <div class='mp'>
        <span>手机号</span>
        <input type="text" placeholder="" v-model="phone">
        <span :class='{rphone:right_phone}' @click='getNum'>{{time?'剩下'+time+'秒':'获取验证码'}}</span>
        	//利用time是否为0动态显示内容
        	//利用计算属性,添加符合时的验证码样式
      </div>
      <div class='mp2'>
        <span>验证码</span>
        <input type="text">
      </div>
    </div>





      </div>
  </div>
</template>

<script>
  import Vue from 'vue';
  import { Switch } from 'vant';

  Vue.use(Switch);

  export default{
    data()
    {
      return{
          flag:true,
          checked:false,
          type:'password',
          phone:'',
          time:0, //倒计时
          timeFlag:true  //是否能够再次发送验证码
      }
    },
    methods:{
      getNum()
      {

        if(this.right_phone) //调用计算属性,点击时是否能够触发验证码
        { 
          if(this.timeFlag)  //倒计时期间的旗帜
          {
            this.timeFlag=false;
            this.time=60;
            let timer=setInterval(()=>{

              this.time--;
              if(this.time==0)
              {
                this.timeFlag=true;
                clearInterval(timer);
              }
            },1000)
          }

        }else{
          alert('请输入正确手机号');
        }
      }
    },
    computed:{ //计算属性动态验证手机号,其中手机号v-model双向绑定
      right_phone()
      {
        return /^1\d{10}$/.test(this.phone) //正则匹配是否11个数字,返回布尔值
      }
    }

  }
</script>

<style lang='less'>


    .mlog{
      margin-top: 20px;
      text-align: start;
      .mp{
        width: 100%;
        height: 40px;
        line-height: 40px;
        white-space: nowrap;
        margin-bottom: 20px;
        >span:last-child{
          color:#ccc;
          margin-left: -40px;

        }
        border: solid 1px #ccc;
        >input{
          border: none;
        }
        .rphone{  //手机号格式正确时的验证码样式
          color:black !important;

        }
      }
      

}
</style>

效果图:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值