0
点赞
收藏
分享

微信扫一扫

【vue input文本框自动去空格和特殊字符 】全局自定义指令

青乌 2022-01-27 阅读 60
// main.js 注册一个全局自定义指令 `不允许输入空格&*等特殊字符`  指令:v-filter-special-char
Vue.directive('filterSpecialChar', {
  update: function (el, { value, modifiers }, vnode) {
          try {
             let a_el=el.children[0]
              //此处可以debug看看el具体值是什么,这里演示的是原生控件input,如果是使用element中的<el-input />标签,需要通过 el.children[0] 拿到原生input.
                  if(!a_el.value){
                     return false; 
                   }
                   a_el.value = a_el.value.replace(/[^a-zA-Z0-9\u4E00-\u9FA5#@%]/g, "");
                   a_el.dispatchEvent(new Event(modifiers.lazy ? 'change' : 'input'))
          } catch (e) {
          }
  }
})

用法

 					<a-input
                        allow-clear
                        placeholder="请输入"
                        v-model="queryParam.keywords"
                        v-filter-special-char
                      ></a-input>
举报

相关推荐

0 条评论