Vue + ElementPlus 自定义指令控制输入框只可以输入数字

自定义指令 v-number-only

1. 创建指令文件

src/directives/numberOnly.ts

ts 复制代码
/**
 * 自定义指令
 * @description 输入框只允许输入数字
 * @example
 * <el-input v-model="num" v-number-only placeholder="只能输入数字" />
 */
import type { Directive } from 'vue'

export const numberOnly: Directive<HTMLInputElement> = {
  mounted(el) {
    const input = el.querySelector('input')!
    input.addEventListener('input', () => {
      // 使用正则替换掉所有非数字字符
      input.value = input.value.replace(/\D/g, '')
      // 触发 v-model 更新
      input.dispatchEvent(new Event('input'))
    })
  }
}

2. 注册指令

main.ts 中注册全局指令:

ts 复制代码
import { createApp } from 'vue'
import App from './App.vue'
import { numberOnly } from './directives/numberOnly'

const app = createApp(App)

app.directive('number-only', numberOnly)

app.mount('#app')

3. 使用方式

在任何 el-input 上都可以直接使用:

vue 复制代码
<template>
  <el-input v-model="num" v-number-only placeholder="只能输入数字" />
</template>

<script setup lang="ts">
import { ref } from 'vue'

const num = ref('')
</script>

拓展

支持数字 + 小数点 + 负号

ts 复制代码
/**
 * 数字 + 小数点 + 负号
 */
export const numberWithDecimal: Directive<HTMLInputElement> = {
  mounted(el) {
    const input = el.querySelector('input')!
    input.addEventListener('input', () => {
      let val = input.value
      // 允许开头负号
      val = val.replace(/[^0-9.-]/g, '')
      // 只允许一个负号(且必须在开头)
      val = val.replace(/(?!^)-/g, '')
      // 只允许一个小数点
      val = val.replace(/(\..*)\./g, '$1')

      input.value = val
      input.dispatchEvent(new Event('input'))
    })
  }
}

支持千分位格式化

ts 复制代码
/**
 * 千分位格式化(只允许数字)
 * 输入 1234567 -> 显示 1,234,567
 */
export const numberThousands: Directive<HTMLInputElement> = {
  mounted(el) {
    const input = el.querySelector('input')!
    input.addEventListener('input', () => {
      // 去掉所有非数字
      let raw = input.value.replace(/\D/g, '')
      if (!raw) {
        input.value = ''
      } else {
        // 格式化成千分位
        input.value = Number(raw).toLocaleString()
      }
      input.dispatchEvent(new Event('input'))
    })
  }
}

只允许输入中文

ts 复制代码
/**
 * 只允许输入中文
 */
export const chineseOnly: Directive<HTMLInputElement> = {
  mounted(el) {
    const input = el.querySelector('input')!
    input.addEventListener('input', () => {
      // 过滤掉所有非中文字符(\u4e00-\u9fa5 是中文字符范围)
      input.value = input.value.replace(/[^\u4e00-\u9fa5]/g, '')
      input.dispatchEvent(new Event('input'))
    })
  }
}
相关推荐
行云流水6263 小时前
uniapp pinia实现数据持久化插件
前端·javascript·uni-app
zhangyao9403303 小时前
uniapp动态修改 顶部导航栏标题和右侧按钮权限显示隐藏
前端·javascript·uni-app
福尔摩斯张5 小时前
Axios源码深度解析:前端请求库设计精髓
c语言·开发语言·前端·数据结构·游戏·排序算法
aiguangyuan5 小时前
React 中什么是可中断更新?
javascript·react·前端开发
李牧九丶5 小时前
从零学算法1334
前端·算法
1***s6325 小时前
JavaScript微服务
javascript·微服务·devops
周周爱喝粥呀5 小时前
UI设计原则和Nielsen 的 10 条可用性原则
前端·ui
小云朵爱编程6 小时前
Vue项目Iconify的使用以及自定义图标,封装图标选择器
前端·javascript·vue.js
前端大卫6 小时前
CSS 属性值 initial、unset 和 revert 的解析
前端