el-input添加自定义指令只允许输入中文/英文/数字,兼容输入法事件

省流

script:

javascript 复制代码
directives: {
 regexp: {
   inserted: (el, binding, vnode) => {
     let composition = false
     const formatValue = function (e) {
       if (composition) return
       // vnode.componentInstance组件实例
       vnode.componentInstance.$emit('input', e.target.value.replace(/[^\u4e00-\u9fa5a-zA-Z0-9/]/g, ''))
     }
     el.oninput = formatValue
     // 请务必使用addEventListenner,不信邪你试试el.oncompositionstart
     el.addEventListener('compositionstart', () => { composition = true })
     el.addEventListener('compositionend', e => {
       composition = false
       formatValue(e)
     })
   }
 }
}

template:

html 复制代码
<el-input v-regexp />

有问题欢迎指出

相关推荐
GIS之路21 分钟前
GDAL 实现创建几何对象
前端
liulilittle35 分钟前
CLANG 交叉编译
linux·服务器·开发语言·前端·c++
自信阿杜1 小时前
跨标签页数据同步完全指南:如何选择最优通信方案
前端·javascript
牛马1111 小时前
WidgetsFlutterBinding.ensureInitialized()在 Flutter Web 端启动流程的影响
java·前端·flutter
Captaincc1 小时前
2025: The year in LLMs
前端·vibecoding
指尖跳动的光1 小时前
Vue的nextTick()方法
前端·javascript·vue.js
码事漫谈2 小时前
可能,AI早都觉醒了
前端
qq_406176142 小时前
JS 事件循环(Event Loop)
开发语言·前端·javascript
Codebee2 小时前
ooder A2UI ES6版本正式发布:现代化模块架构,MIT开源许可
前端
Devin_chen2 小时前
4.前端使用Node + MongoDB + Langchain消息管理与聊天历史存储
前端·langchain