网络编程
位置:首页>> 网络编程>> JavaScript>> Vue使用vux-ui自定义表单验证遇到的问题及解决方法

Vue使用vux-ui自定义表单验证遇到的问题及解决方法

作者:Lucia_Huang  发布时间:2024-05-10 14:18:07 

标签:vux-ui,vue,表单验证

初学框架vue搭配vux使用发现这个UI库使用有些力不从心。下面说说自己在表单验证过程遇到的两个需求问题及解决的方法。

1.使用x-input组件可知,官方只给了三种类型的is-type验证器,分别是:email,china-name,china-mobile,其他需要自己自定义验证器,怎么写验证器?

解决方法:自定义is-type验证器(试验过可以在valid使用正则验证)


<x-input type="number" v-model="code" placeholder="请输入验证码" :is-type="codeValue" />
export default {
 data() {
   return{
     code: '',
     codeValue: function(value){
       return {
         valid: value.length === 4,
         msg: "验证码有误!"
       }
     }
   }
 }
}

2.表单内容都填写无误之后,提交表单的按钮才能被触发(如图)

Vue使用vux-ui自定义表单验证遇到的问题及解决方法

解决方法:使用x-input组件的@on-change事件,及ref属性


<x-input type="number" v-model="code" placeholder="请输入验证码" :is-type="codeValue" ref="refcode" @on-change="keyDown" />
<x-button action-type="submit" :disabled="disabled">完成</x-button>
export default {
   data() {
     return{
       code: '',
       disabled: true,
       codeValue: function(value){
         return {
           valid: value.length === 4,
           msg: "验证码有误!"
         }
       }
     }
   },
   methods: {
     keyDown(){
       if(this.$refs.refcode.valid == true && this.code != ''){
         this.disabled = false;
       }else{
         this.disabled = true;
       }
     }
   }
 }

总结

以上所述是小编给大家介绍的Vue使用vux-ui自定义表单验证遇到的问题及解决方法网站的支持!

来源:https://www.jianshu.com/p/0ab3d10cc104

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com