export default {
bind(el, binding, vnode) {
const input =
el.querySelector('.el-input__inner') ||
el.querySelector('.el-textarea__inner') ||
el
input.addEventListener('compositionstart', () => {
vnode.locking = true
})
input.addEventListener('compositionend', () => {
vnode.locking = false
input.dispatchEvent(new Event('input'))
})
input.onkeyup = () => {
if (vnode.locking) {
return
}
if (binding.modifiers.num) {
onlyNum(input)
}
else if (binding.modifiers.num_point) {
onlyNumPoint(input)
}
else if (binding.modifiers.float) {
onlyFloat(input, binding.value)
}
else if (binding.modifiers.int) {
onlyInt(input)
}
else if (binding.modifiers.intp) {
onlyIntp(input)
}
else if (binding.modifiers.alp) {
onlyAlp(input)
}
else if (binding.modifiers.num_alp) {
onlyNumAlp(input)
}
else if (binding.modifiers.text) {
onlyText(input)
}
else if (binding.modifiers.arith) {
onlyArith(input)
}
input.dispatchEvent(new Event('input'))
}
function onlyNum(input) {
input.value = input.value.replace(/\D+/g, '')
}
function onlyInt(input) {
let value = input.value
value = value.replace(/\D+/g, '')
input.value = value ? Number(value).toString() : value
}
function onlyIntp(input) {
if (!/^[1-9][0-9]*$/.test(input.value)) {
let value = input.value.replace(/\D+/g, '')
if (value && value.substring(0, 1) === '0') {
value = value.substring(1)
}
input.value = value
}
}
function onlyNumPoint(input) {
input.value = input.value.replace(/[^\d.]/g, '')
}
function onlyFloat(input, n) {
let value = input.value
value = value.replace(/[^\d.]/g, '')
value = value.replace(/^\./g, '')
value = value
.replace('.', '$#$')
.replace(/\./g, '')
.replace('$#$', '.')
if (n && Number(n) > 0) {
var d = new Array(Number(n)).fill(`\\d`).join('')
var reg = new RegExp(`^(\\-)*(\\d+)\\.(${d}).*$`, 'ig')
value = value.replace(reg, '$1$2.$3')
}
if (value && !value.includes('.')) {
value = Number(value).toString()
}
input.value = value
}
function onlyAlp(input) {
input.value = input.value.replace(/[^A-Za-z]/g, '')
}
function onlyNumAlp(input) {
input.value = input.value.replace(/[^A-Za-z0-9]/g, '')
}
function onlyText(input) {
input.value = input.value.replace(/[^A-Za-z0-9\u4E00-\u9FA5]/g, '')
}
function onlyArith(input) {
const value = input.value
if (value) {
input.value = value.split('').reduce((prev, cur) => {
if (/^[\d|\-|\+|\*|\/|\.|\(|\)]+$/.test(cur)) {
return prev + cur
}
return prev
}, '')
}
}
}
}
console