vue中判断电话号码的正则及验证

<template>
    <div>
        <h1>{{msg}}</h1>
        <form action="" method="get" class="form">
            <div class="input">
                <input type="text" v-model="number" @input="isPhone()" maxlength="11" autofocus placeholder="请输入手机号码"> 
                <input type="button" @click="changeMsg" v-model="message" :disabled="disabled" >
                <div class="errorColor" v-show="isShow">电话号码输入有误,请重新输入</div>
                <div><input type="text" name="" id="" placeholder="请输入验证码"></div>
                <input type="submit" value="提交">
            </div>
            
            
            </form>
    </div>




</template>

<script>
    export default{
        data(){
            return{
                msg:"短信验证码",
                number:'',
                message:'发送验证码',
                disabled: true,
                time: 5,
                isShow: false,
            }
        },
        methods:{
            isPhone(){
               var reg = /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/;
                if(this.number.length ==11){
                    if(!reg.test(this.number)){
                        this.isShow = true;
                    }else {
                        this.disabled = false;
                        console.log('电话号码输入正确!');
                    }
                }else {
                    this.disabled = true;
                    this.isShow = false;
                }
                // console.log(reg)
                
            },
            changeMsg(){
                
                this.disabled = true;
                this.message = this.time + 's后重新获取';
                var timer = setInterval(()=>{
                    this.time--;
                    this.time = this.time>=0?this.time:0;
                    this.message = this.time + 's后重新获取';
                    if(this.time == 0){
                        this.message = '重新获取';
                        this.disabled = false;
                        clearInterval(timer);
                        this.time = 5;
                    }
                },1000)
                
                
            }
        }
    }
</script>

<style scoped>
h1 {
    text-align: center;
}
.input {
    text-align: initial;
    width: 285px;
    margin: 0 auto;
}
.form {
    width: 100%;
    margin: 0 auto;
}
input {
    outline: none;
    margin-top: 20px;
    margin-right: 20px;
}
input::placeholder {
    text-indent: 10px;
}
input[type=text] {
    height: 30px;
    text-indent: 10px;
}
input[type=submit]{
    width: 100%;
    height: 30px;
    margin: 20px auto 0;
}
input[type=button] {
    display:inline-block;
    width:90px;
    height: 34px;
    margin-right:0;
}
.errorColor {
    color: red;
    margin-top: 10px;
}
</style>

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值