输入框限制只能输入手机号码,并且以3-4-4格式分开

data(){
    return{
        form: {
          uscuName: null, // 学生
          uscuPhone: null, // 注册手机号
          sex: -1, // 性别
          camId: null, // 学籍校区id
          campusName: null, // 学籍校区
          grade: null, // 年级 
          courseEducationalType: null, // 学年制 
          uscuThirdCode: null, // 用户三方库编码 
      },
    }
},
// 使用监听器的格式来控制手机号码只能输入数字
watch:{
'form.uscuPhone':{
      handler(newValue, oldValue) {
        let oldLength = oldValue ? oldValue.length : 0;
        //  newValue.replace(/\s/g, '') 去掉空格; 
        // replace(/[^\d]/g,'') 去掉非数字的字符; 
        //replace(/(\d{3})(\d{0,4})(\d{0,4})/, '$1 $2 $3') 分割成 3-4-4 格式的手机号;
        this.form.uscuPhone = 
         newValue.length > oldLength ? 
          newValue.replace(/\s/g, '').replace(/[^\d]/g,'').replace(/(\d{3})(\d{0,4})    (\d{0,4})/, '$1 $2 $3') 
            : this.form.uscuPhone.trim();
        if (this.form.uscuPhone.length > 13) {
          // 验证/保存的手机号码,截取13位(其中包含两个空格)
          this.form.uscuPhone = this.form.uscuPhone.substring(0,13);
        } 
        this.telphoneTrue = this.form.uscuPhone.length === 13 ? true : false;
       
      },
      deep:true
    },
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值