vue中手机号,邮箱正则验证以及60s发送验证码

今天写了一个简单的验证,本来前面用的组件,但是感觉写的组件在此项目不是很好用,由于用到的地方比较少,所以直接写在了页面中。页面展示如图

<div>
    <p class="fl">
        <input  name="phone" type="number" placeholder="手机号" v-model="phone"/>
        <button type="button" :disabled="disabled" @click="sendcode" class="btns">{{btntxt}}</button>
    </p>
    <p class="fl" style="margin-left: 20px;">
        <input type="text" placeholder="验证码"/>
    </p>
</div>
<input type="button" value="查询"  class="btns search" @click="query"/>

这里是script里的内容

export default {
          data: function () {
            return {
                disabled:false,
                time:0,
                btntxt:"获取验证码",
                formMess:{
                    email:this.email,
                    phone:this.phone
                }
            }
          },
          mounted: function () {
              
          },
        methods:{
            //验证手机号码部分
            sendcode(){
                var reg=11 && /^((13|14|15|17|18)[0-9]{1}\d{8})$/;
                //var url="/nptOfficialWebsite/apply/sendSms?mobile="+this.ruleForm.phone;
                if(this.phone==''){
                   alert("请输入手机号码");
                }else if(!reg.test(this.phone)){
                    alert("手机格式不正确");
                }else{
                    this.time=60;
                    this.disabled=true;
                    this.timer();
                    /*axios.post(url).then(
                        res=>{
                        this.phonedata=res.data;
                    })*/
               }
            },
            timer() {
                if (this.time > 0) {
                     this.time--;
                     this.btntxt=this.time+"s后重新获取";
                     setTimeout(this.timer, 1000);
                } else{
                     this.time=0;
                     this.btntxt="获取验证码";
                     this.disabled=false;
                }
            },
            query(){
                var formMess=this.formMess
                Axios.post(api+"/order/select/reception", formMess)
                 .then(function (res) {
                     if(res.data.code==200){
                         console.log(res.data.data);
                          this.productResult=res.data.data;
                          this.productResult.length=3;
                     }else if(res.data.code==400){
                         alert(res.data.message)
                     }
                      
                  }.bind(this))
            },
            //邮箱验证
            sendEmail(){
                var regEmail= /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
                if(this.email==''){
                   alert("请输入邮箱");
                }else if(!regEmail.test(this.email)){
                    alert("邮箱格式不正确");
                }
            }
        }
    }
  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值