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 } })