vue中表单实时验证

1.不能输入汉字

 <x-input v-model="userInfo.newPwd" class="user__input" :type="pwdShow? 'text':'password'" placeholder="请输入密码" @input='inputFun'></x-input>  //绑定inputFun事件

//在methods中定义函数
methods: {inputFun(e){
  let arr = []
  let arr2 = []
  let val = ''
  let isR = false
  arr.push(...e)
  //正则验证,将字符串拆分每一个字进行验证,防止一次性输入多个汉字
  arr.map((v)=>{  
     if(/[\u4E00-\u9FA5]/.test(v)){      
        isR = true
     }else{
          arr2.push(v)
        }
     })
     if(isR){
        this.$vux.toast.text('请勿输入汉字');
        val = arr2.join('')       
        this.$nextTick(function(){        //在这时渲染,实时替换
          this.userInfo.newPwd = val
        })
     }     
   }
 }  
//也可以在
 watch: {
    userInfo.newPwd:function(){}  //绑定要监听的
 }  

猜你喜欢

转载自blog.csdn.net/qq_28353055/article/details/83619703