• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

v-onlyNumber在vue限制输入框的最大最小值以和小数位数

武飞扬头像
一狐九
帮助1

使用方法:可用于el-input

<input type="number" v-only-number={
	max:'999',最大值
	min:'0',最小值
	precision:'0',保留几位小数
	noZero:true,为空的时候是0还是字符串''
}>

index.js

export default {
  inserted(el, binding, vNode) {
    el.vDir = binding
    // vDir.value 有指令的参数
    let content
    // 设置输入框的值,触发input事件,改变v-model绑定的值
    const setVal = val => {
      if (vNode.componentInstance) {
      // 如果是自定义组件就触发自定义组件的input事件
        vNode.componentInstance.$emit('input', val)
      } else {
      // 如果是原生组件就触发原生组件的input事件
        el.value = val
        el.dispatchEvent(new Event('input'))
      }
    }
    // 输入框输入时
    el.addEventListener('input', event => {
      const vDir = el.vDir
      const e = event || window.event
      content = e.target.value
      if (!content) {
        return
      }
      let arg_max = ''
      let arg_min = ''
      if (vDir.value) {
        arg_max = parseFloat(vDir.value.max)
        arg_min = parseFloat(vDir.value.min)
      }
      if (arg_max !== undefined && content > arg_max) {
        setVal(arg_max)
        content = arg_max
        return
      }
      if (arg_min !== undefined && content < arg_min) {
        setVal(arg_min)
        content = arg_min
        return
      }
      let arg_precision = 0 // 默认保留至整数
      if (vDir.value.precision) {
        arg_precision = parseFloat(vDir.value.precision)
      }
      const reg = RegExp(`(?<=\\.\\d{${arg_precision}}).*$`)
      content = String(content).replace(reg, '')
      setVal(content)
    })
    // 按键按下=>只允许输入 数字/小数点/减号
    el.addEventListener('keypress', event => {
      const e = event || window.event
      const inputKey = String.fromCharCode(typeof e.charCode === 'number' ? e.charCode : e.keyCode)
      const re = /\d|\./
      content = e.target.value
      // 定义方法,阻止输入
      function preventInput() {
        if (e.preventDefault) {
          e.preventDefault()
        } else {
          e.returnValue = false
        }
      }
      if (!re.test(inputKey) && !e.ctrlKey) {
        preventInput()
      } else if (content.indexOf('.') > 0 && inputKey === '.') {
      // 已有小数点,再次输入小数点或者只允许输入整数
        preventInput()
      }
    })
    // 输入框失去焦点时
    el.addEventListener('focusout', event => {
      const vDir = el.vDir
      const e = event || window.event
      content = parseFloat(e.target.value)
      if (!content) {
        content = vDir.value.noZero ? '' : 0 // vDir.value.noZero 可以清空输入框,不填充0
      }
      let arg_precision = 0 // 默认保留至整数
      if (vDir.value.precision) {
        arg_precision = parseFloat(vDir.value.precision)
      }
      const reg = RegExp(`(?<=\\.\\d{${arg_precision}}).*$`)
      content = String(content).replace(reg, '')
      setVal(content)
    })
  },
  install(Vue) {
    Vue.directive('onlyNumber', this)
  }
}
学新通

main.js

import Vue from 'vue'
import onlyNumber from 'index.js'
Vue.use(onlyNumber)

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfjaaag
系列文章
更多 icon
同类精品
更多 icon
继续加载