duhuizhe
2023-10-16 3aa55dd3f62cee2c1c4c0aa74e1570acf83f8927
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import Vue from 'vue'
 
Vue.directive('float', function (el, binding) {
  const {value} = binding
  const min = value.min || 0
  const max = value.max || 9999999
  const digit = value.digit || 2
  const _input = el
  _input.onkeyup = function (e) {
    let value = e.currentTarget.firstElementChild.value
    if (value.length == 1) {
      value = value.replace(/[^1-9]/g, '')
    } else {
      if (digit == 1) {
        value = value.replace(/^0[0-9]+/, val => val[1])
          .replace(/^(\.)+/, '')
          .replace(/[^\d.]/g, '')
          .replace(/\.+/, '.')
          .replace(/^(\-)*(\d+)\.(\d).*$/, '$1$2.$3')
      } else {
        value = value.replace(/^0[0-9]+/, val => val[1])
          .replace(/^(\.)+/, '')
          .replace(/[^\d.]/g, '')
          .replace(/\.+/, '.')
          .replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3')
      }
    }
    if (min) {
      if (value < min) {
        value = min
      }
    }
    if (max) {
      if (value > max) {
        value = max
      }
    }
    e.currentTarget.firstElementChild.value = value
  }
})